Skip to content

fix(ai-proxy): don't abort Anthropic response on bad tool_call arguments#13599

Open
AlinsRan wants to merge 4 commits into
apache:masterfrom
AlinsRan:fix/anthropic-converter-toolcall-decode
Open

fix(ai-proxy): don't abort Anthropic response on bad tool_call arguments#13599
AlinsRan wants to merge 4 commits into
apache:masterfrom
AlinsRan:fix/anthropic-converter-toolcall-decode

Conversation

@AlinsRan

Copy link
Copy Markdown
Contributor

Description

In the Anthropic-messages → OpenAI-chat converter, when an OpenAI-compatible upstream returns tool_call arguments that are not valid JSON (or decode to a non-object such as a number/string), convert_response did return nil, "invalid tool_call arguments: ...". This aborts the entire non-streaming response conversion and drops any already-collected text/thinking content — the client receives nothing instead of a partial-but-usable message.

This patch falls back to an empty input object ({}) and logs a warning, so the rest of the message (text, other tool calls) is still delivered. Anthropic's tool_use.input must be an object, so non-object decoded values are handled the same way as a decode failure.

Added t/plugin/ai-proxy-anthropic.t TEST 52 covering malformed arguments: conversion still returns, text content is preserved, tool_use.input is an empty object, and a warning is logged.

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have verified that this change is backward compatible

When an OpenAI-compatible upstream returns tool_call arguments that are not
valid JSON (or not a JSON object), convert_response previously returned
`nil, err`, aborting the entire non-streaming response conversion and dropping
any already-collected text/thinking content -- the client received nothing.

Fall back to an empty input object and log a warning instead, so the rest of
the message (text, other tool calls) is still delivered. Anthropic tool_use
input must be an object, so non-object decoded values are treated the same way.

Signed-off-by: AlinsRan <alinsran@apache.org>
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels Jun 23, 2026
AlinsRan added 2 commits June 24, 2026 13:11
…rter-toolcall-decode

Signed-off-by: AlinsRan <alinsran@apache.org>

# Conflicts:
#	t/plugin/ai-proxy-anthropic.t
Add TEST 55 exercising tool_call arguments that are valid JSON but decode
to a non-object value (e.g. "123"), covering the "not a JSON object"
fallback branch where err is nil.

Signed-off-by: AlinsRan <alinsran@apache.org>
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Jun 24, 2026
TEST 55 now also verifies already-collected text content survives the
empty-object fallback, symmetric with TEST 54.

Signed-off-by: AlinsRan <alinsran@apache.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants