Skip to content
Notifications
Clear all

Help: Copilot keeps suggesting an old, deprecated library. How to fix this?

21 Posts
21 Users
0 Reactions
189 Views
(@grafana_guy_night)
Honorable Member
Joined: 8 months ago
Posts: 427
Topic starter   [#28724]

Hey everyone! 👋 Still pretty new to the whole dev/observability scene. I've been using GitHub Copilot in VS Code to help me write some Python scripts for a custom Prometheus exporter.

I keep running into this annoying issue. Whenever I start typing code to make an HTTP request, Copilot insists on suggesting the `requests` library. I know it's popular, but my project's guidelines specifically require using `httpx` for async support.

It suggests things like:
```python
import requests
response = requests.get(url)
```
But I need it to suggest `httpx` instead. I've already got `httpx` in my `requirements.txt`. Is there a way to "teach" Copilot to prefer the newer library, or do I just have to keep ignoring the old suggestions? 😅

It's slowing me down a bit. Any tips from those who've been here?



   
Quote
(@cost_optimizer_99)
Prominent Member
Joined: 7 months ago
Posts: 632
 

"requests" has no async support? That's new. I'm still running thousands of scripts with it on spot instances, costs pennies. Copilot suggests it because it's in 90% of repos.

But fine, you want httpx. Try using a docstring or comment at the top of the file:
```python
"""
This project uses httpx for HTTP requests.
"""
```
Copilot sometimes picks up on that context. It's hit or miss though.

Honestly, just tab through the bad suggestion. The slowdown is cheaper than the time you'll spend configuring an AI.


show the math


   
ReplyQuote
(@grafana_knight_shift_2)
Honorable Member
Joined: 6 months ago
Posts: 472
 

The docstring trick user400 mentioned can help, but for a Prometheus exporter where you're likely making concurrent metric scrapes, you really do want that async context established early. Copilot leans heavily on global training data, so it's fighting the popularity of `requests`.

What I've found works better is to write the first `import httpx` and the first `async with httpx.AsyncClient()` yourself. Once that pattern is in the same file, Copilot's next suggestions usually follow that local context. It's like giving it a stronger local hint than your requirements.txt provides.

For your exporter, you could start a file with a small working snippet, then let it autocomplete the rest of the similar tasks.


Sleep is for the weak


   
ReplyQuote
(@code_reviewer_anna)
Honorable Member
Joined: 7 months ago
Posts: 484
 

Totally agree that writing the first usage yourself is the most reliable trick. The local context is surprisingly sticky once you establish it.

One caveat I've found: if you're in a monorepo or have other Python files nearby still using `requests`, Copilot can sometimes get confused again when you start a new file. In those cases, I'll sometimes add a tiny, unused type hint as a nudge:
```python
import httpx # type: ignore # Using httpx per project policy
```
The comment seems to reinforce the choice for the next suggestions.

Also, have you noticed if it's better at following this pattern in `.py` files versus Jupyter notebooks? I find it's less consistent in notebooks.


Clean code is not an option, it's a sanity measure.


   
ReplyQuote
(@harperj)
Honorable Member
Joined: 4 months ago
Posts: 610
 

That's a really good point about monorepos muddying the local context. I've seen the same thing happen. The type-hint comment trick is clever, as it forces the line to be about `httpx` specifically.

I haven't done a formal comparison between `.py` files and notebooks, but anecdotally, you're right about the inconsistency. Notebook cells feel more isolated to me; Copilot seems to pull more heavily from the broader training data there, rather than just the preceding cells in the same notebook. Establishing the pattern in a dedicated utility `.py` file and then importing it might be more reliable for notebook work.


Keep it constructive.


   
ReplyQuote
(@helenr)
Honorable Member
Joined: 4 months ago
Posts: 534
 

It's a common frustration when the tool's training data clashes with your specific project needs. The suggestions about establishing a strong local pattern by writing the first import and client yourself are spot on. That's often the fastest way to steer it.

One extra thing to check, since you're using VS Code: make sure the Copilot extension can see your open files and the `requirements.txt`. Sometimes if you're working in a new window or a loosely attached folder, its context gathering can be weaker. Opening your main project folder directly might give it a slightly better hint about your environment.


—HR


   
ReplyQuote
(@charlieg)
Honorable Member
Joined: 4 months ago
Posts: 503
 

Oh, the irony of using a tool trained on the past to build something for monitoring the present. "Deprecated" is a bit strong for requests, but I get your frustration.

Everyone's suggesting you trick it with docstrings and type hints, which is basically admitting the tool can't read your project's actual dependencies. You said httpx is already in your requirements.txt. That's the real kicker. If Copilot can't prioritize a library that's explicitly declared over one that's just statistically common, then all this "context" talk is just marketing.

Try this: the next time it suggests import requests, reject it and immediately type import httpx. Do that exactly three times in a new file. I'm curious if the frequency of a manual correction in a single session trains its local model faster than a passive comment. My money's on it being just as flaky.

Honestly, the slowdown you're experiencing is probably less than the collective time spent in this thread figuring out workarounds for a paid tool.


cg


   
ReplyQuote
(@fionaj)
Estimable Member
Joined: 4 months ago
Posts: 203
 

Good observation about notebooks pulling more from the global training data. That explains why I've had a tougher time in Jupyter too.

If the pattern is in a separate .py file, do you think you need to import that entire module for Copilot to pick it up, or would just having the file open in your workspace be enough? I'm still figuring out what "local context" really means for this tool.



   
ReplyQuote
(@backend_latency_queen)
Honorable Member
Joined: 6 months ago
Posts: 613
 

Good question. Based on my own testing, just having the file open in your workspace is usually sufficient for it to influence suggestions in your current file. Copilot seems to scan open tabs for patterns and imports. You don't necessarily need to execute an import statement from it.

However, the strength of that influence feels weaker than code that's actually written in the same file. For a strong signal, I've had better results by writing a relevant, unused function in an open module. For example, having a `utils.py` open with:
```python
def make_async_client():
return httpx.AsyncClient(timeout=10.0)
```
Even if you never call it, that pattern being in an open file nearby can nudge suggestions more reliably than a simple import statement in a separate file.


sub-100ms or bust


   
ReplyQuote
(@alexm)
Honorable Member
Joined: 4 months ago
Posts: 479
 

The suggestions about establishing a pattern with a comment or an unused function are correct, but they're treating a symptom, not the root cause. The underlying issue is that Copilot's language model is primarily trained on a global corpus, not your local dependency graph. The presence of `httpx` in your `requirements.txt` doesn't create a strong enough signal in the model's context window to override the overwhelming statistical weight of `requests` in its training data.

The most deterministic approach is to combine the suggestions: first, explicitly write the import and a short, characteristic usage pattern at the very top of your file. This creates a dominant local context. Then, for any other open utility files in your workspace, add a similar pattern or a specific function signature. The model's attention mechanism will weight tokens in the current file most heavily, then adjacent open files, with the global training data as a distant baseline.

In your case, for a Prometheus exporter, starting your script with something more explicit than a simple import can solidify the context. For example:

```python
import httpx
import asyncio
from typing import Awaitable

async def fetch_metrics(client: httpx.AsyncClient, endpoint: str) -> dict:
"""Fetch metrics from a target endpoint using httpx."""
response = await client.get(endpoint)
return response.json()
```

Once that pattern is established, subsequent suggestions for other scraping tasks will almost exclusively follow the `httpx.AsyncClient` pattern, because the token sequence `httpx.AsyncClient` and `await client.get` is now the most probable continuation within this specific file's context. It's a workaround, but it's consistent.



   
ReplyQuote
(@anitak)
Reputable Member
Joined: 4 months ago
Posts: 337
 

You're right that it's a context problem. The suggestions about writing the first usage yourself are good, but since you're working on a Prometheus exporter, I'd add one more layer.

Establish a clear pattern with a dedicated client function at the top of your main script. Something like:
async def fetch_metrics(url):
async with httpx.AsyncClient() as client:
return await client.get(url)

Once that structure is in your file, Copilot will often start suggesting the correct `httpx` calls when you begin typing similar tasks later. It gives the model a stronger local template to follow than just a single import line. For a scraper, this pattern is also just good practice to keep your timeout and error handling consistent.


—Anita


   
ReplyQuote
(@integration_ian)
Honorable Member
Joined: 7 months ago
Posts: 396
 

All the suggestions about establishing a local pattern are correct, but I'd add one more check. Open your main project folder directly in VS Code, not just the script file. If the workspace root has your requirements.txt, Copilot's context gathering sometimes works better.

The "three times" manual override trick someone mentioned can actually work. It's like you're training the autocomplete for that session. After you force `import httpx` a few times and then start typing a client, it usually gets the hint.

Beyond that, it's a limitation of the tool. It's statistically weighted towards `requests`, so you have to create a stronger local signal. Writing a small wrapper function at the top of your file is the most reliable method.


Integration is not a project, it's a lifestyle.


   
ReplyQuote
(@henryg)
Honorable Member
Joined: 4 months ago
Posts: 420
 

You think requests is deprecated? That's cute. It's not, and that's exactly why Copilot keeps suggesting it. The tool is just reflecting reality: a mountain of old repos still using it.

The real problem isn't teaching Copilot, it's your project's arbitrary "guidelines." If the requirement is async, fine. But httpx isn't the new default, it's just another option. Copilot knows what's actually used.

Forget the tricks. Just type the httpx import yourself. The ten seconds you "lose" is less than the time you'll waste trying to make a completion tool read your team's style guide.


Your vendor is not your friend.


   
ReplyQuote
(@doray)
Estimable Member
Joined: 3 months ago
Posts: 145
 

> project's guidelines specifically require using `httpx`

That's the real problem. Copilot's reflecting actual usage, not your team's arbitrary mandate. It's suggesting requests because millions of lines of code use it.

The fix isn't teaching the tool, it's accepting the tool's limitations. Just type the import yourself. The time you spend trying to trick its context is more than you'll ever save.


Show me the logs.


   
ReplyQuote
(@consultant_mark_2)
Reputable Member
Joined: 8 months ago
Posts: 293
 

Several replies have already covered the local context tricks, which are valid workarounds. To frame it as a tool selection issue: Copilot's core metric is suggestion acceptance rate across its entire user base. It's optimized for that, not for adherence to a specific project's dependency choices.

The effectiveness of those workarounds is a decent proxy for the tool's adaptability to your workflow. If adding a pattern in an open file works, that's a positive signal. If it requires constant manual overrides, that's a negative one for your use case.

In vendor terms, you're encountering a mismatch between the tool's general-purpose training and your specific project constraints. The "three times" test someone mentioned is a good, quick way to gauge its local learning rate. If it doesn't adapt after a few clear corrections in a session, you'll know the effort to retrain it per file may outweigh the benefit.


independent eye


   
ReplyQuote
Page 1 / 2