OpenAI API Migration: Step-by-Step Guide
In this article
To migrate to the OpenAI API, you need to update your code to use the latest API endpoints, models, and authentication methods, then test and deploy the changes. The exact steps depend on which version you are migrating from (e.g., from the older Completions API to the newer Responses API or from codex models to newer ones). Below is a detailed walkthrough covering the common migration paths.
Background: Why Migrate?
OpenAI periodically updates its API to improve performance, add features, and deprecate older models. For example, the text-davinci-003 model was deprecated in favor of GPT-3.5 Turbo and later GPT-4 models. Similarly, the older /v1/completions endpoint is being phased out in favor of the /v1/chat/completions endpoint, which supports multi-turn conversations and is more flexible. Migrating ensures you get better results, lower latency, and access to new capabilities like function calling and structured outputs.
How It Works: Key API Differences
The core change in most migrations is the request format. The old Completions API took a single prompt string and returned a single text completion. The Chat Completions API takes a list of messages (each with role and content) and returns a message object. For example:
- Old:
prompt: "Translate this to French: Hello" - New:
messages: [{role: "user", content: "Translate this to French: Hello"}]
Also, the authentication method remains the same (Bearer token in the Authorization header), but the model names have changed (e.g., gpt-3.5-turbo instead of text-davinci-003). The Responses API (introduced later) combines chat and tool-use into a single endpoint, but for most users, migrating to Chat Completions is the primary step.
Step-by-Step Migration Process
- Review your current usage: Identify which API endpoints and models your code calls. Check your logs or codebase for
/v1/completionsand model names likedavinci. - Update the request format: Replace the
promptparameter with amessagesarray. For simple prompts, wrap your text in{role: "user", content: "..."}. If you used system-level instructions, add asystemmessage. - Change the model name: Use a current model like
gpt-4o-miniorgpt-4o. Check OpenAI's model list for the latest options. - Adjust parameters: Some parameters like
max_tokensare still valid, buttemperatureandtop_pwork the same. Note thatstopsequences are still supported. - Handle the response: Instead of reading
data.choices[0].text, readdata.choices[0].message.content. - Test thoroughly: Run your updated code with a variety of inputs, including edge cases like empty prompts or long conversations.
- Deploy and monitor: Roll out the change to production, and monitor for errors or unexpected behavior.
How to Choose the Right Migration Path
Your migration path depends on what you are currently using. If you are on the Completions API, move to Chat Completions. If you are already on Chat Completions but using an older model (like gpt-3.5-turbo-0301), you may just need to update the model name to a newer version. If you are using the Assistants API or the Responses API, the steps differ. In general, check OpenAI's official migration guides for the specific deprecation dates and recommended replacements. For most users, the simplest path is to update the request format and model name, then test.
Common Problems and Misconceptions
- Prompt vs. messages: A common mistake is to still send a
promptfield, which will cause a 400 error. Ensure you usemessages. - Model availability: Some deprecated models are no longer available, so you must switch to a current one. Check the API response for errors like "model not found".
- Token limits: Chat models have different token limits. For example,
gpt-4osupports 128k tokens, but if you send a large prompt, you may hit the limit. Adjust your input accordingly. - Misconception: You must use the Responses API. While the Responses API is the newest, the Chat Completions API is still fully supported and will remain so for the foreseeable future. You can migrate to Chat Completions and stay there.
Costs and Pricing Considerations
Migration can affect your costs because newer models have different pricing per token. For example, GPT-4 models are more expensive than GPT-3.5 Turbo, but they may require fewer tokens for the same task due to better instruction following. To estimate costs, multiply the number of input and output tokens by the per-token price for your chosen model. OpenAI's pricing page provides current rates. During migration, test with a small sample to measure token usage and compare costs with your previous setup.
Safety and Best Practices
When migrating, ensure you handle API keys securely—never expose them in client-side code. Use environment variables or a backend proxy. Also, validate all user inputs before sending them to the API to avoid prompt injection attacks. If your application handles sensitive data, review OpenAI's data usage policies; the API does not use your data for training by default, but you should confirm your compliance. Finally, implement error handling for rate limits (HTTP 429) and server errors (5xx) with retry logic and exponential backoff.
Frequently Asked Questions
Do I need to migrate from Completions to Chat Completions?
Yes, if you are still using the /v1/completions endpoint, because OpenAI has deprecated many of the models that work with it, and the endpoint itself is being phased out. The Chat Completions API is the recommended replacement and offers more features.
What is the difference between Chat Completions and the Responses API?
The Chat Completions API is designed for chat-based interactions, while the Responses API is a newer, unified endpoint that combines chat, tool use, and other features into a single interface. If you are starting fresh, you might choose the Responses API, but migrating from Completions to Chat Completions is a simpler first step.
How do I update my code from text-davinci-003 to gpt-3.5-turbo?
Change the model name to gpt-3.5-turbo and convert your prompt to a messages array. For example, {prompt: "Hello"} becomes {messages: [{role: "user", content: "Hello"}]}. The response will be in choices[0].message.content instead of choices[0].text.
Will my API key still work after migration?
Yes, your API key remains the same. You do not need to regenerate it unless you suspect a security breach. The key is used in the Authorization header for all API versions.
Can I use the same parameters like temperature and max_tokens?
Yes, these parameters are still supported in the Chat Completions API. However, note that max_tokens is now called max_completion_tokens in the Responses API, but in Chat Completions it remains max_tokens.
What happens if I don't migrate?
Eventually, the deprecated endpoints and models will stop working. OpenAI typically announces a deprecation date, after which requests will fail. You will need to migrate before that date to avoid service interruption.