Skip to main content

Posts

Showing posts with the label Python SDK

Migrating from OpenAI to Anthropic API: Fixing 400 Errors and System Prompts

  You have a production-ready application running on GPT-4. You decide to integrate Claude 3.5 Sonnet for its reasoning capabilities or cost profile. You swap the SDK, change the API key, and update the model name. You run the code, expecting a seamless transition. Instead, your logs explode with  anthropic.BadRequestError . The error messages are specific but frustrating: "messages: roles must alternate between 'user' and 'assistant'" or "max_tokens is required". This is not a simple drop-in replacement. While OpenAI and Anthropic share similar underlying concepts, their API architectures differ strictly regarding conversation history and parameter enforcement. This guide provides a root-cause analysis of these failures and a robust, reusable Python solution to normalize your message history for the Anthropic SDK. The Root Cause: Loose vs. Strict State Machines To fix the code, you must understand the architectural divergence between the two provide...