All jobs

Fix openai.ChatCompletion Errors by Migrating to the v1 Client

Yes. An agent reproduces the APIRemovedInV1 error, ports the code to the current OpenAI client, tests it against a stub server and opens a pull request.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The error usually arrives after a fresh install, so the agent reproduces it the same way. In an app like logankilpatrick/ChatGPT-Simple, requirements.txt is three unpinned lines: python-dotenv, openai, flask. server.py sets openai.api_key = os.getenv("OPENAI_API_KEY"), and its /get_response route calls openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[...]) and returns completion["choices"][0]["message"]["content"]. The agent installs the requirements into a clean environment, which resolved to openai 3.46.0 and Flask 3.1.3 when this page was written, and calls both routes through Flask's test client. / answers 200. /get_response answers 500 with APIRemovedInV1: You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0. The page loads and the first message fails, which is why this reaches users before anyone notices.

The message offers two exits, pinning openai==0.28 or running openai migrate. The agent checks the second and finds that this install puts no openai command in the environment's bin, so it ports by hand. That is three edits here: from openai import OpenAI and a client = OpenAI(), client.chat.completions.create(...) with the same arguments, and attribute access on the result. The last one is easy to miss, because the call succeeds and the next line fails. Left as completion["choices"], it raises TypeError: 'ChatCompletion' object is not subscriptable.

No key is needed to prove the port. The client reads OPENAI_BASE_URL, so the agent starts a small HTTP server in the sandbox that answers POST /v1/chat/completions with a canned completion, points the client at it and writes pytest tests around the route. They assert the reply text, the path, the Authorization: Bearer header and the JSON body, which should hold the same model and messages as before. Then it uses the stub for the cases the old code never handled. A response whose content is null makes the view return None, and Flask answers 500. A request with no message parameter sends "content": null upstream. A 429 from the stub is retried: the client makes three calls before raising RateLimitError, because its default is two retries.

Two behaviours change with the new client and the agent reports both. The old module-level assignment tolerated a missing key until the first call. OpenAI() raises OpenAIError: Missing credentials when it is constructed, so with the client at module level python server.py no longer starts without OPENAI_API_KEY. The agent keeps that, since failing at start is easier to notice, and says so. The default timeout allows 600 seconds for a read, which for a web request is effectively none, so it sets an explicit timeout and max_retries on the client and names the values in the description. It pins openai to the major version it tested, the step that would have prevented the original break.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "This Python app fails with an error that says openai.ChatCompletion is no longer supported in openai>=1.0.0. Fix it by migrating to the current OpenAI client; do not pin the old 0.28 release. Read the requirement files and every file that imports openai. Create a clean virtual environment, install the requirements exactly as written, record the versions that resolve, and reproduce the error by calling each route with the framework's test client. List every legacy call: module-level api_key or api_base, ChatCompletion, Completion, Embedding, error classes under openai.error, dictionary access on responses. Port each to a client object and attribute access. You have no API key: start a local stub HTTP server that answers the chat completions path, set OPENAI_BASE_URL to it, and write pytest tests for the route that assert the reply, the request path, the Authorization header and the JSON body. Use the stub to show what the route does for a null content, a missing input parameter and a 429, including how many upstream calls the client makes. Report whether the app still starts with no key set. Set an explicit timeout and retry count on the client. Pin openai in the requirements to the major version you tested. Do not change the model name; say that it was not verified against the live API. Open a pull request with the error before, every command and the test output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request that changes server.py and requirements.txt and adds a test file with its stub server. The description opens with the reproduced traceback and the resolved versions, lists the three edits, and shows the test output. A short table covers the edge cases: null content, missing parameter and a rate limit, with the status the route returns for each and the number of upstream calls. It states the two behaviour changes, start-up without a key and the retry default, and the timeout it chose.

A reviewer should know what was not exercised. Nothing in the run talks to the real API, so the model name gpt-3.5-turbo is carried over unverified, and the description says that in its first paragraph. The first live call happens in your environment with your key. The agent also notes without changing it that server.py ends with app.run(debug=True), which serves an interactive debugger on errors and should not face the internet.

Run it on every error alert naming APIRemovedInV1

This failure announces itself in logs with a fixed string, so it is a good one to wire to an alert. In the console, add an automation to the Project with a webhook trigger and a saved prompt like the one above; the console shows the trigger's secret once. Sentry signs its own webhooks and cannot send a custom Authorization header, and other trackers vary, so point the tracker at a small handler on your server and have the handler call the trigger's URL with Authorization: Bearer <secret> and the alert as the body. The body, up to 64 KB, is added after the prompt as event text.

Send an Idempotency-Key header on the call to the trigger's URL, set to the alert's id, so a repeated delivery is answered deduped and starts nothing new. The automation's max runs per hour is the backstop if an alert storm gets through.

The use case this job follows, with its automation and trigger: Fix a bug from an alert.

Why run it on Rush?

The fix is three lines and the risk is in everything around them, which only shows when the code runs. Reproducing the error needs a clean install that resolves today's versions, not the ones cached on a developer's machine. Proving the fix needs a server to talk to. The run does both in its own sandbox, with a stub in place of the API so no key of yours is involved, and keeps the output of each step.

It also means the pull request arrives with tests the repository did not have. The stub and the assertions on path, header and body stay in the codebase, so the next major release of the client is caught by CI on a version bump and not by a user's first message.

Frequently asked questions

Can I just pin openai==0.28?

It stops the error, and the message itself suggests it. You then stay on the 0.28 line while the client, the models and their parameters move on. The request above asks for the port because for an app this size it is a few lines; pinning is reasonable as a same-day stopgap.

What does openai.ChatCompletion.create become?

client.chat.completions.create(...) on a client made with OpenAI(), with the same model and messages arguments. The result is an object, so completion["choices"][0]["message"]["content"] becomes completion.choices[0].message.content. Errors move from openai.error.RateLimitError to openai.RateLimitError.

How can it be tested without an API key?

The client sends its requests to whatever OPENAI_BASE_URL names. The agent runs a local HTTP server that returns a fixed completion, sets a placeholder key, and asserts on what the app sent and what it returned. No request leaves the sandbox.

Does the new client retry on its own?

Yes. Its default is two retries, so a rate-limited call is attempted three times before the exception reaches your code. That can hide short outages and lengthen slow ones, which is why the agent sets max_retries and timeout explicitly instead of inheriting the defaults.

More OpenAI Python SDK jobs

More jobs like this: Fix a bug from an alert

Keep reading