nic-6443 opened a new pull request, #13321:
URL: https://github.com/apache/apisix/pull/13321
## Description
This PR rewrites the Anthropic Messages → OpenAI Chat Completions converter
using a whitelist body construction approach instead of the previous
`core.table.clone()` (blacklist) approach.
The core issue: the old code cloned the entire Anthropic request and then
tried to delete/rename fields. This meant any unhandled Anthropic-specific
fields (metadata, top_k, raw thinking config, output_config, etc.) leaked
through to the upstream OpenAI-compatible provider, causing unexpected behavior
or errors.
The fix builds the outgoing OpenAI body from scratch (`openai_body = {}`),
explicitly converting only the fields that have OpenAI equivalents. This is the
same approach used by other protocol bridges (litellm, higress).
## Changes
**Bug fixes:**
- Empty tools array no longer produces a `tools` field in the forwarded
request
- `max_tokens` correctly maps to `max_completion_tokens` (never forwards raw
`max_tokens`)
- `output_config`/`output_format` properly converts to `response_format`
- Error responses from upstream pass through as Anthropic error format (not
generic 500)
**New features:**
- Image and document (PDF) content block conversion (base64 → data URL)
- `tool_choice` conversion (auto/any/tool/none → OpenAI equivalents)
- `thinking` config → `reasoning_effort` (low/medium/high by budget
threshold)
- thinking/redacted_thinking history blocks preserved in message content
- `tool_result` with multimodal content (text + image mixed)
- `reasoning_content`/`reasoning` extraction in non-streaming responses →
thinking block
- Streaming reasoning/thinking blocks with proper content_block events
- `cache_control` preservation on content blocks and tool definitions
- Billing header `cch=` stripping from system prompts
- Header conversion (`x-api-key` → Bearer, remove anthropic-*/x-stainless-*
headers)
- `disable_parallel_tool_use` → `parallel_tool_calls=false`
- Cached tokens handling (deduct from input_tokens, report
cache_read/creation)
- Finish reason normalization (nil/empty/"null" not treated as stop)
- Stream usage deferred flush with trailing chunk merge
- Dynamic content_block index counter across thinking/text/tool blocks
**Tests:**
- Expanded from 9 to 40 test cases covering all conversion scenarios
- Added 4 new fixture files for tool calls, reasoning, errors, and
multi-tool responses
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]