Skip to content

fix(openai): don't run tool calls with truncated or malformed arguments - #8192

Open
yibol9768-alt wants to merge 1 commit into
google:mainfrom
yibol9768-alt:fix/openai-llm-malformed-tool-calls
Open

yibol9768-alt wants to merge 1 commit into
google:mainfrom
yibol9768-alt:fix/openai-llm-malformed-tool-calls

Conversation

@yibol9768-alt

Copy link
Copy Markdown

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

2. Or, if no issue exists, describe the change:

Problem:
In OpenAILlm (src/google/adk/integrations/openai/_openai_llm.py), tool-call arguments that are not valid JSON, or that parse to something other than a JSON object, become args={} and the call is returned as a normal success, in both the non-streaming path (_tool_call_parts) and the streaming path. This includes a call cut off by max_tokens (finish_reason="length"). The flow then runs the tool with no arguments, so a tool whose parameters are all optional runs with its defaults.

Solution:
Same policy as the other adapters (#7803 for LiteLlm, #7686 for Apigee, #7558 for Claude):

  • arguments that are truncated (finish_reason="length") return error_code=MAX_TOKENS and no function calls;
  • arguments that are not a JSON object return error_code=MALFORMED_FUNCTION_CALL (so ReflectAndRetryModelPlugin can retry) and no function calls;
  • if one call in a batch is bad, none of the batch is returned, so a valid call does not run next to a broken one;
  • an empty arguments string still means {} for no-argument tools, and text content is kept.

Raw arguments are not logged, only the tool name and length.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

New tests in tests/unittests/integrations/openai/test_openai_llm.py cover truncated and malformed arguments ([], null, "x", invalid JSON) in both paths, a batch with text plus a truncated call, and valid / empty arguments still succeeding. 11 of them fail on main; with this change pytest tests/unittests/integrations/openai/ gives 306 passed. pyink, isort, ruff and mypy pass on the changed file.

Manual End-to-End (E2E) Tests:

vllm serve Qwen/Qwen3-1.7B --enable-auto-tool-choice --tool-call-parser hermes on a local RTX 5090, OpenAILlm(base_url=...), a write_note(text, path) tool, max_output_tokens=30, so the call is cut off mid-arguments (finish_reason="length"):

main this PR
streaming function_calls=[('write_note', {})], error_code=None function_calls=[], error_code=MAX_TOKENS
non-streaming vLLM returns the cut call as text, no tool_calls; same on both same

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

This is independent of the reasoning changes discussed in #8191 and touches only the tool-call parsing in _openai_llm.py.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants