Skip to content

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

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

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
@GWeale

GWeale commented Sep 11, 2026

Copy link
Copy Markdown
Collaborator

fixed on main in da22e43, which validates every buffered tool call regardless of finish reason. it reports malformed_function_call rather than max_tokens, so your test won't pass as written. not in 2.8.0. open a new issue if you see anything like this again.

@GWeale GWeale closed this Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants