Skip to content

base_url parameter using os.environ.get("ANTHROPIC_BASE_URL") by default #985

Closed as not planned
@mbary

Description

@mbary

Long story short:

Before trying out the Anthropic python sdk, I was using the OpenAI SDK.

The openai sdk requires

client = OpenAI(
    api_key="ANTHROPIC_API_KEY",  # Your Anthropic API key
    base_url="https://api.anthropic.com/v1/"  # Anthropic's API endpoint
)

So for ease of use, I've added a environmental variable

export ANTHROPIC_BASE_URL='https://api.anthropic.com/v1/'

Little did I know, the name I chose would cause issues down the line.
Today, I tried using the anthropic sdk, using the default setup

from anthropic import Anthropic
client = Anthropic()

but kept getting an extremely vague error

NotFoundError: Error code: 404 - {'type': 'error', 'error': {'type': 'not_found_error', 'message': 'Not Found'}}

I knew the API key was valid because:

  1. It worked with openai sdk
  2. curl commands didn't return any errors

After multiple back and forth messages with Claude embedded in the docs, he suggested to explicitly set the base_url parameter to "https://api.anthropic.com"
and voila, it works perfectly.

Going through the python sdk code, I noticed the following if-statement

if base_url is None:
    base_url = os.environ.get("ANTHROPIC_BASE_URL")
if base_url is None:
    base_url = f"https://api.anthropic.com"

So the issue was that the client, by default, read a variable with an incorrect value (for this particular use case).

My question is:

Is loading base_url, by default, from environment variables necessary?
Especially if the correct value is then hard coded and passed as an argument.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions