Skip to content

Sentry Integration Causing '431 Request Header Fields Too Large' Error with FastAPI and Azure API Management #3526

@Amirshox

Description

@Amirshox

I'm encountering an issue where enabling Sentry in my FastAPI application causes a 431 Request Header Fields Too Large error when requests are processed through Azure API Management (used for load balancing). The problem disappears when Sentry is disabled, indicating that Sentry might be adding additional data to the headers, causing them to exceed size limits.

Here is the relevant Sentry and Azure OpenAI integration code:

import sentry_sdk
from openai import AzureOpenAI

# Sentry initialization
sentry_sdk.init(
    dsn=SENTRY_DSN,
    environment=ENVIRONMENT,
    traces_sample_rate=0.5,
    profiles_sample_rate=0.5,
)

# Azure OpenAI client setup
client = AzureOpenAI(
    api_key=OPENAI_API_KEY,
    api_version=OPENAI_API_VERSION,
    azure_endpoint=OPENAI_URL,
)

try:
    response = client.chat.completions.create(
        model=model,
        messages=prompt,
        response_format={"type": "json_object"},
        temperature=temperature,
        max_tokens=max_tokens,
        tools=tools,
        tool_choice="auto"
    )
    output = response.choices[0].message.tool_calls[0].function.arguments
except Exception as e:
    print(f"Error: {e}")

Disabling Sentry resolves the issue, and the request is processed as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No fields configured for issues without a type.

    Projects

    Status

    Waiting for: Product Owner

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions