Repository navigation
fix(llm): tolerate unparseable stored tool-call arguments in Anthropic/Google/AWS formatters - #6321
Conversation
eaa86ee to
3e886e8
Compare
β¦ formats The Anthropic, Google, and AWS chat-context formatters call json.loads(msg.arguments) directly. FunctionCall.arguments is only canonicalized to valid JSON when the model output parses; unparseable output (unrecoverable open-weight calls, or history restored via ChatContext.from_dict) persists verbatim, so formatting that history on a later turn raised json.JSONDecodeError and aborted the whole request. Add a shared parse_tool_call_arguments() helper that falls back to an empty object (with a warning) when the arguments aren't a JSON object, and use it in the three formatters. OpenAI/Mistral already pass arguments through as an opaque string and are unaffected. Fixes livekit#6308 Co-Authored-By: Claude Opus 4.8 <[email protected]>
3e886e8 to
6cb0d1c
Compare
|
@longcw rebased onto main so the conflict's cleared and CI's green again. small one β the anthropic/google/aws formatters call json.loads on stored tool-call args, so a call whose args never parsed takes down a later turn with a JSONDecodeError; this routes them through a shared helper that falls back to {} instead. mind a look when you get a sec? no rush |
longcw
left a comment
There was a problem hiding this comment.
thanks for the pr! could you rebase it to main and something nit:
| if not arguments: | ||
| return {} | ||
| try: | ||
| parsed = json.loads(arguments) |
There was a problem hiding this comment.
use parse_function_arguments from llm/utils.py?
β¦rguments
Per review: reuse llm/utils.py's parse_function_arguments instead of a local
json.loads. It raises ValueError when it cannot reach a dict, which is the right
contract for a live call the model can retry but not for replaying history, so
the wrapper catches that and falls back to {}.
Strictly better than the bare json.loads it replaces: json_repair, chat-template
token stripping and double-encoded unwrapping now apply to stored history too, so
arguments that would have been discarded are recovered instead. Covered by three
new cases (trailing comma, <|end|> leak, double-encoded); all nine fail against
the previous implementation.
The import is inside the function on purpose: llm/utils.py imports chat_context,
which imports this package, so a module-level import is a circular import.
Developed with Claude Code; reviewed and tested by Pranav before pushing.
|
gimme 15 |
|
both done, and your nit made the fix better than it was. rebase. merged current main (259 commits). the only conflict was
that turns out to be a real upgrade over the
that matters more than it looks: one thing worth a second of your attention since it looks like sloppiness otherwise: the import is inside the function, deliberately. at module scope it's a cycle β
gate on the merged tree: your approval carried over the push, so this is yours to merge whenever β I don't have write access here. |
This PR contains:
What is the current behavior? (You can also link to an open issue here)
Fixes #6308.
FunctionCall.argumentsholds the model's raw tool-call output and is only canonicalized to valid JSON when it parses successfully. When it can't be parsed β an open-weight model's output thatjson_repaircan't recover (the call fails as aToolErrorand the raw string persists), or history built outside the execution path (ChatContext.from_dict, a customllm_node, restored session state) β the raw string stays in chat history verbatim.The Anthropic, Google, and AWS chat-context formatters call
json.loads(msg.arguments)directly into_chat_ctx. A later, healthy turn that formats that history then raisesjson.JSONDecodeErrorand the whole request aborts. OpenAI and Mistral passargumentsthrough as an opaque string, so they don't hit this.What is the new behavior?
Adds a shared
parse_tool_call_arguments()helper in_provider_format/utils.pythat returns the parsed dict when the stored arguments are a JSON object, and otherwise logs a warning and falls back to an empty object{}. The three JSON-object formatters call it instead ofjson.loads(...).{}is preferred over a best-effort repair: the historical tool call already produced its output, so fabricating arguments the model never sent risks feeding wrong data back to the provider. A tool call's arguments are always a named-parameter object, so JSON that parses to a non-object (array, scalar,null) degrades the same way.Tests added in
tests/test_chat_ctx.pycover all three providers against unparseable arguments, a valid-args passthrough regression, and empty/non-object arguments. The unparseable/non-object cases fail onmain(raiseJSONDecodeError) and pass with this change.Does this PR introduce a breaking change? (What changes might users need to make in their application due to this PR?)
No. Valid arguments format exactly as before; only previously-crashing inputs change behavior β from a raised
JSONDecodeErrorto an empty-args object plus a warning log.Other information:
Developed with Claude Code; reviewed and tested by Pranav before marking ready for review.