This commit fixes the issue where the output_format parameter was not properly handled in the /v1/messages endpoint for Claude models on Azure Foundry, Amazon Bedrock, and other providers. Changes: 1. Added output_format field to AnthropicMessagesRequestOptionalParams TypedDict to prevent it from being stripped from requests 2. Added "output_format" to the list of supported parameters in get_supported_anthropic_messages_params() 3. Updated _update_headers_with_anthropic_beta() to automatically inject the structured-outputs-2025-11-13 beta header when output_format is present 4. Added comprehensive test suite to verify structured outputs functionality The fix applies to all providers using the /v1/messages endpoint: - Anthropic (direct) - Amazon Bedrock - Azure Foundry (Azure AI) - Vertex AI All these implementations inherit from AnthropicMessagesConfig, so the fix automatically propagates to all of them. Fixes issue where structured outputs returned Markdown text instead of JSON when using /v1/messages endpoint, even though direct provider API calls worked correctly.
4.6 KiB
Fix for Structured Outputs in /v1/messages Endpoint
Issue Description
The /v1/messages endpoint for Claude Sonnet 4.5 deployed in Azure Foundry and Amazon Bedrock was not properly handling the output_format parameter for structured outputs. When users sent requests with output_format, the response was Markdown text instead of JSON, even though direct calls to the provider APIs worked correctly.
Root Cause
The output_format parameter was not recognized as a valid parameter in the /v1/messages endpoint implementation. Specifically:
-
Missing from TypedDict:
output_formatwas not included in theAnthropicMessagesRequestOptionalParamsTypedDict, causing it to be stripped from requests. -
Missing from supported params: The
get_supported_anthropic_messages_params()method inAnthropicMessagesConfigdid not include"output_format"in its list of supported parameters. -
Missing beta header injection: The
_update_headers_with_anthropic_beta()method did not automatically add thestructured-outputs-2025-11-13beta header whenoutput_formatwas present.
Files Changed
1. /home/user/litellm/litellm/types/llms/anthropic.py
Change: Added output_format field to AnthropicMessagesRequestOptionalParams TypedDict
class AnthropicMessagesRequestOptionalParams(TypedDict, total=False):
# ... existing fields ...
output_format: Optional[AnthropicOutputSchema] # Structured outputs support
2. /home/user/litellm/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py
Changes:
a) Added "output_format" to supported parameters list:
def get_supported_anthropic_messages_params(self, model: str) -> list:
return [
# ... existing params ...
"output_format",
# ...
]
b) Updated _update_headers_with_anthropic_beta() to inject structured-outputs beta header:
# Check for structured outputs
if optional_params.get("output_format") is not None:
beta_values.add(ANTHROPIC_BETA_HEADER_VALUES.STRUCTURED_OUTPUT_2025_09_25.value)
3. /home/user/litellm/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_messages_structured_outputs.py
Change: Created comprehensive test suite to verify structured outputs support
Tests include:
- Verification that
output_formatis in supported parameters - Request transformation preserves
output_format - Beta header is automatically added
- Beta headers merge correctly with existing headers
- Integration test for full request flow
- Specific tests for Bedrock and Azure Foundry models
Impact
This fix applies to all providers that use the /v1/messages endpoint, including:
- Anthropic (direct API calls)
- Amazon Bedrock (via
AmazonAnthropicClaudeMessagesConfigwhich inherits fromAnthropicMessagesConfig) - Azure Foundry (via
AzureAnthropicMessagesConfigwhich inherits fromAnthropicMessagesConfig) - Vertex AI (via
VertexAIPartnerModelsAnthropicMessagesConfigwhich inherits fromAnthropicMessagesConfig)
All these implementations inherit from AnthropicMessagesConfig, so the fix automatically propagates to all of them.
Verification
The fix ensures that:
- The
output_formatparameter is preserved throughout the request pipeline - The
anthropic-beta: structured-outputs-2025-11-13header is automatically injected - The complete request (including
output_formatin the body and the beta header) is sent to the provider API - Structured outputs work correctly for Claude Sonnet 4.5 and Opus 4.1 models on all supported providers
Example Usage
After this fix, users can use structured outputs with the /v1/messages endpoint:
curl --request POST \
--url https://litellm.example.com/v1/messages \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--header "X-API-KEY: <api-key>" \
-d '{
"model": "claude-sonnet-4-5",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Extract the key information from this email: John Smith (john@example.com) is interested in our Enterprise plan."
}
],
"output_format": {
"type": "json_schema",
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"email": {"type": "string"},
"plan_interest": {"type": "string"}
},
"required": ["name", "email", "plan_interest"],
"additionalProperties": false
}
}
}'
This will now correctly return JSON output instead of Markdown text.