How would they reliably recognize the context needed without the necessary context?
in the case of hallucinating a library, give it access to an IDE's autocomplete or type checker or whatever so it can check if the functions it thinks exist actually do, if they don't, start feeding it documentation or type info about the library until it spits out stuff that type checks
For other stuff this is obviously harder.