fix(ai-proxy): drop tool_choice without tools and stop hanging Anthropic stream#13583
Open
AlinsRan wants to merge 1 commit into
Open
fix(ai-proxy): drop tool_choice without tools and stop hanging Anthropic stream#13583AlinsRan wants to merge 1 commit into
AlinsRan wants to merge 1 commit into
Conversation
…pic stream The Anthropic-to-OpenAI converter had two bugs: - tool_choice (and parallel_tool_calls) were forwarded even when no tools reached the upstream -- either none were provided or all were dropped as Anthropic built-ins / invalid -- which makes OpenAI-compatible upstreams reject the request. Drop them when the tools array ends up empty. - On a streaming [DONE] that arrives after message_start but before any content block was opened (no finish_reason chunk), convert_sse_events returned nil without emitting message_stop, leaving the client hanging. Emit message_delta + message_stop whenever the message was already started. Add unit tests for both cases in t/plugin/ai-proxy-anthropic.t. Signed-off-by: AlinsRan <alinsran@apache.org>
nic-6443
approved these changes
Jun 22, 2026
membphis
approved these changes
Jun 22, 2026
membphis
left a comment
Member
There was a problem hiding this comment.
Approved. No blocking issues found.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Two bugs in the Anthropic-to-OpenAI converter (
apisix/plugins/ai-protocols/converters/anthropic-messages-to-openai-chat.lua):tool_choiceforwarded withouttools→ upstream 400.tool_choice(andparallel_tool_calls) were sent to the OpenAI-compatible upstream even when no tools ended up in the request — either the client provided none, or all were dropped as Anthropic built-ins (e.g.web_search) / invalid. OpenAI-compatible upstreams rejecttool_choicewithouttools. They are now dropped when the tools array is empty.Streaming client hang. When a stream emitted
message_startbut the upstream sent[DONE]before any content block opened and without afinish_reasonchunk,convert_sse_eventsreturnedniland never emittedmessage_stop, leaving the client hanging until timeout.message_delta+message_stopare now emitted whenever the message was already started.Tests
Added two unit tests to
t/plugin/ai-proxy-anthropic.t:tool_choice/parallel_tool_callsdropped when no tools are forwarded (no-tools and all-built-in cases); preserved when a real tool remains.[DONE]aftermessage_startwithout a content block still emitsmessage_stop.Checklist