Skip to content

fix(anthropic_endpoints): serialize dict-detail HTTPExceptions on /v1/messages like sibling surfaces - #38741

Merged
mateo-berri merged 1 commit into
litellm_internal_stagingfrom
litellm_fix_anthropic_messages_dict_detail_error
Aug 29, 2026
Merged

mateo-berri merged 1 commit into
litellm_internal_stagingfrom
litellm_fix_anthropic_messages_dict_detail_error

Conversation

@mateo-berri

@mateo-berri mateo-berri commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • /v1/messages returns guardrail dict-detail 400s as a python-repr blob
  • /v1/chat/completions and /v1/responses serialize the same error cleanly
  • clients cannot parse the block reason on /v1/messages

How it solves it:

  • /v1/messages now uses the same error serialization as its sibling surfaces
  • clean message plus the full details under provider_specific_fields

User Flow

Before: a developer whose post_call guardrail blocks a request on /v1/messages gets a stringified python dict as the error message instead of the guardrail's clean message

  1. They send POST https://litellm-domain/v1/messages with {"model": "claude-sonnet-5", "max_tokens": 128, "messages": [{"role": "user", "content": "Say the word kumquat"}]} while a guardrail that blocks "kumquat" is on
  2. The response is HTTP 400 with "message": "400: {'error': \"Content blocked: keyword 'kumquat' detected\", 'keyword': 'kumquat', 'guardrail': 'keyword-block', ...}", a python-repr blob their client cannot parse, and no provider_specific_fields
  3. The same prompt on POST https://litellm-domain/v1/chat/completions comes back HTTP 400 with "message": "Content blocked: keyword 'kumquat' detected" and the full details under provider_specific_fields, so the two surfaces disagree

After: the same blocked request on /v1/messages returns the same clean error as the other endpoints

  1. They send the same POST https://litellm-domain/v1/messages with the "kumquat" prompt and the same guardrail on
  2. The response is HTTP 400 with "message": "Content blocked: keyword 'kumquat' detected" and the keyword, guardrail name, and mode under provider_specific_fields
  3. POST https://litellm-domain/v1/chat/completions and POST https://litellm-domain/v1/responses return the identical error shape, so all three surfaces agree

Relevant issues

Pre-existing bug found during LIT-6410 QA on PR #38721. That PR is in flight on the same subsystem but touches neither of the files changed here, so there is no dependency either way

Linear ticket

Resolves LIT-6466

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Live proxy QA, no mocks, real Anthropic API calls costing real money. Both legs ran the same config (a post_call custom guardrail keyword-block, default_on, raising HTTPException(status_code=400, detail={"error": "Content blocked: keyword 'kumquat' detected", "keyword": "kumquat", "guardrail": "keyword-block"}) when "kumquat" appears in the request) on the same topology, 1 instance x 2 uvicorn workers (--num_workers 2), only the proxy commit differing: before = merge base ae7e50f on port 30846, after = this PR's tip 817b5f4 on port 23226. The blocked /v1/messages request was sent twice per leg to cover both workers and the outputs were byte-identical each time

Before (ae7e50f)

POST /v1/messages happy path

  1. Command:
curl -sS -X POST http://localhost:30846/v1/messages -H 'Authorization: Bearer sk-1234' -H 'Content-Type: application/json' -d '{"model": "claude-sonnet-5", "max_tokens": 128, "messages": [{"role": "user", "content": "Say hello in one short sentence"}]}' -w '\nHTTP_STATUS:%{http_code}\n'
  1. Output:
{"model":"claude-sonnet-5","id":"msg_011CeWgANMgyW5syLc3gDJ7z","type":"message","role":"assistant","content":[{"type":"text","text":"Hello!"}],"stop_reason":"end_turn","stop_sequence":null,"stop_details":null,"usage":{"input_tokens":16,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0},"output_tokens":6,"output_tokens_details":{"thinking_tokens":0},"service_tier":"standard","inference_geo":"global"}}
HTTP_STATUS:200

POST /v1/messages blocked prompt

  1. Command:
curl -sS -X POST http://localhost:30846/v1/messages -H 'Authorization: Bearer sk-1234' -H 'Content-Type: application/json' -d '{"model": "claude-sonnet-5", "max_tokens": 128, "messages": [{"role": "user", "content": "Say the word kumquat"}]}' -w '\nHTTP_STATUS:%{http_code}\n'
  1. Output, the stringified python dict:
{"error":{"message":"400: {'error': \"Content blocked: keyword 'kumquat' detected\", 'keyword': 'kumquat', 'guardrail': 'keyword-block', 'guardrail_name': 'keyword-block', 'guardrail_mode': 'post_call'}","type":"None","param":"None","code":"400"}}
HTTP_STATUS:400

POST /v1/chat/completions blocked prompt

  1. Command:
curl -sS -X POST http://localhost:30846/v1/chat/completions -H 'Authorization: Bearer sk-1234' -H 'Content-Type: application/json' -d '{"model": "claude-sonnet-5", "messages": [{"role": "user", "content": "Say the word kumquat"}]}' -w '\nHTTP_STATUS:%{http_code}\n'
  1. Output, clean on the same proxy:
{"error":{"message":"Content blocked: keyword 'kumquat' detected","type":"None","param":"None","code":"400","provider_specific_fields":{"error":"Content blocked: keyword 'kumquat' detected","keyword":"kumquat","guardrail":"keyword-block","guardrail_name":"keyword-block","guardrail_mode":"post_call"}}}
HTTP_STATUS:400

POST /v1/responses blocked prompt

  1. Command:
curl -sS -X POST http://localhost:30846/v1/responses -H 'Authorization: Bearer sk-1234' -H 'Content-Type: application/json' -d '{"model": "claude-sonnet-5", "input": "Say the word kumquat"}' -w '\nHTTP_STATUS:%{http_code}\n'
  1. Output, also clean, so at the base only /v1/messages mangles the detail:
{"error":{"message":"Content blocked: keyword 'kumquat' detected","type":"None","param":"None","code":"400","provider_specific_fields":{"error":"Content blocked: keyword 'kumquat' detected","keyword":"kumquat","guardrail":"keyword-block","guardrail_name":"keyword-block","guardrail_mode":"post_call"}}}
HTTP_STATUS:400

After (817b5f4)

POST /v1/messages happy path

  1. Command:
curl -sS -X POST http://localhost:23226/v1/messages -H 'Authorization: Bearer sk-1234' -H 'Content-Type: application/json' -d '{"model": "claude-sonnet-5", "max_tokens": 128, "messages": [{"role": "user", "content": "Say hello in one short sentence"}]}' -w '\nHTTP_STATUS:%{http_code}\n'
  1. Output:
{"model":"claude-sonnet-5","id":"msg_011CeWgBGBtRk524i66kdrZ9","type":"message","role":"assistant","content":[{"type":"text","text":"Hello! How's your day going?"}],"stop_reason":"end_turn","stop_sequence":null,"stop_details":null,"usage":{"input_tokens":16,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0},"output_tokens":12,"output_tokens_details":{"thinking_tokens":0},"service_tier":"standard","inference_geo":"global"}}
HTTP_STATUS:200

POST /v1/messages blocked prompt

  1. Command:
curl -sS -X POST http://localhost:23226/v1/messages -H 'Authorization: Bearer sk-1234' -H 'Content-Type: application/json' -d '{"model": "claude-sonnet-5", "max_tokens": 128, "messages": [{"role": "user", "content": "Say the word kumquat"}]}' -w '\nHTTP_STATUS:%{http_code}\n'
  1. Output, now the clean message plus provider_specific_fields:
{"error":{"message":"Content blocked: keyword 'kumquat' detected","type":"None","param":"None","code":"400","provider_specific_fields":{"error":"Content blocked: keyword 'kumquat' detected","keyword":"kumquat","guardrail":"keyword-block","guardrail_name":"keyword-block","guardrail_mode":"post_call"}}}
HTTP_STATUS:400

POST /v1/chat/completions blocked prompt

  1. Command:
curl -sS -X POST http://localhost:23226/v1/chat/completions -H 'Authorization: Bearer sk-1234' -H 'Content-Type: application/json' -d '{"model": "claude-sonnet-5", "messages": [{"role": "user", "content": "Say the word kumquat"}]}' -w '\nHTTP_STATUS:%{http_code}\n'
  1. Output, unchanged and byte-identical to /v1/messages above:
{"error":{"message":"Content blocked: keyword 'kumquat' detected","type":"None","param":"None","code":"400","provider_specific_fields":{"error":"Content blocked: keyword 'kumquat' detected","keyword":"kumquat","guardrail":"keyword-block","guardrail_name":"keyword-block","guardrail_mode":"post_call"}}}
HTTP_STATUS:400

POST /v1/responses blocked prompt

  1. Command:
curl -sS -X POST http://localhost:23226/v1/responses -H 'Authorization: Bearer sk-1234' -H 'Content-Type: application/json' -d '{"model": "claude-sonnet-5", "input": "Say the word kumquat"}' -w '\nHTTP_STATUS:%{http_code}\n'
  1. Output, unchanged and byte-identical too, so all three unified endpoints agree:
{"error":{"message":"Content blocked: keyword 'kumquat' detected","type":"None","param":"None","code":"400","provider_specific_fields":{"error":"Content blocked: keyword 'kumquat' detected","keyword":"kumquat","guardrail":"keyword-block","guardrail_name":"keyword-block","guardrail_mode":"post_call"}}}
HTTP_STATUS:400

Type

🐛 Bug Fix

Caveats (if any)

Low

  • any HTTPException on /v1/messages now serializes its detail as the message, not "code: detail"
  • clients matching the old mangled string will see the new clean message
  • error.type and error.param are literal "None" strings: pre-existing on all three surfaces, left alone
  • provider_specific_fields carries guardrail_name and guardrail_mode extras: pre-existing, left alone
  • /v1/messages errors use the OpenAI envelope, not Anthropic's: pre-existing, tracked as LIT-6468

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

  • 817b5f4 passes /live-pr-risk


Note

Low Risk
Narrow error-handling change on the proxy; it improves response shape for guardrail blocks and may change error message text for any HTTPException on /v1/messages, with no auth or data-path changes.

Overview
Fixes inconsistent guardrail error responses on POST /v1/messages. When a guardrail raises HTTPException with a dict detail, that route used to turn the failure into a ProxyException whose message was essentially str(HTTPException) (a Python-repr blob) and omitted structured fields. Chat completions and responses already parsed dict details into a clean message plus provider_specific_fields.

The PR adds shared proxy_exception_from_http_exception in common_request_processing (using existing _serialize_http_exception_detail and merging provider_specific_fields with response headers) and wires anthropic_response’s generic except block to use it for HTTPException before the fallback ProxyException path. The same helper replaces the inlined conversion inside _handle_llm_api_exception, so behavior stays aligned without duplication.

Tests cover the Anthropic endpoint path (LIT-6466) and the helper’s message merge and header pass-through.

Reviewed by Cursor Bugbot for commit 817b5f4. Bugbot is set up for automated code reviews on this repo. Configure here.

@greptile-apps

greptile-apps Bot commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR extracts the existing HTTPException conversion into a shared helper and applies it to Anthropic Messages errors

  • Serializes dictionary details into a clean message with structured provider fields
  • Preserves status codes, headers, and existing provider-specific fields
  • Adds helper-level and endpoint-level regression coverage

Confidence Score: 5/5

The PR appears safe to merge

No blocking failure remains

Important Files Changed

Filename Overview
litellm/proxy/anthropic_endpoints/endpoints.py Routes HTTPException failures through the shared ProxyException serializer while preserving failure-hook execution
litellm/proxy/common_request_processing.py Extracts the established HTTPException conversion into a reusable helper without changing the common processing behavior
tests/test_litellm/proxy/anthropic_endpoints/test_endpoints.py Adds endpoint regression coverage for dictionary-detail HTTPExceptions
tests/test_litellm/proxy/test_common_request_processing.py Covers structured-field merging, header propagation, and plain-string details in the new helper

Reviews (2): Last reviewed commit: "fix(anthropic_endpoints): serialize dict..." | Re-trigger Greptile

@mateo-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@codecov

codecov Bot commented Aug 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mateo-berri

Copy link
Copy Markdown
Contributor Author

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 817b5f4. Configure here.

@mateo-berri
mateo-berri enabled auto-merge August 29, 2026 08:10
@codspeed

codspeed Bot commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_fix_anthropic_messages_dict_detail_error (817b5f4) with litellm_internal_staging (ae7e50f)

Open in CodSpeed

@mateo-berri
mateo-berri merged commit 0c8321e into litellm_internal_staging Aug 29, 2026
87 checks passed
@mateo-berri
mateo-berri deleted the litellm_fix_anthropic_messages_dict_detail_error branch August 29, 2026 23:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants