Skip to content

fix(litellm): validate tool-call JSON on aborted streams too - #6937

Open
lllakshit wants to merge 1 commit into
google:mainfrom
lllakshit:fix/6716-litellm-aborted-stream-tool-call
Open

fix(litellm): validate tool-call JSON on aborted streams too#6937
lllakshit wants to merge 1 commit into
google:mainfrom
lllakshit:fix/6716-litellm-aborted-stream-tool-call

Conversation

@lllakshit

Copy link
Copy Markdown

Summary

  • Validate assembled LiteLLM tool-call arguments unconditionally in _finalize_tool_call_response, not only when finish_reason is length.
  • Return the existing graceful MAX_TOKENS error response when JSON parsing fails, instead of letting a bare JSONDecodeError kill the invocation.
  • Add a regression test for streams that abort mid-tool-call with no terminal finish_reason.

Problem

When a LiteLLM stream aborts mid-tool-call, the stream often ends with no finish_reason. The truncation guard only ran for finish_reason == length, so partial arguments were finalized and json.loads raised an uncaught JSONDecodeError.

This matches the fix shape discussed on #6716 after #6720 was closed.

Test plan

  • pytest tests/unittests/models/test_litellm.py::test_streaming_tool_call_aborted_mid_stream -q
  • pytest tests/unittests/models/test_litellm.py::test_streaming_tool_call_truncated_by_max_tokens -q

Fixes #6716

Made with Cursor

_finalize_tool_call_response only checked malformed tool-call arguments
when finish_reason was length. Streams that abort mid-tool-call end with
no finish_reason, so partial arguments reached json.loads and raised an
uncaught JSONDecodeError that killed the invocation.

Validate assembled arguments unconditionally and return the existing
MAX_TOKENS error response when parsing fails.

Fixes google#6716
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants