Skip to content

fix(responses): lift function_call from mixed assistant content to tool_calls - #29328

Open
hclsys wants to merge 1 commit into
BerriAI:mainfrom
hclsys:fix/responses-mixed-content-function-call
Open

hclsys wants to merge 1 commit into
BerriAI:mainfrom
hclsys:fix/responses-mixed-content-function-call

Conversation

@hclsys

@hclsys hclsys commented May 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #28978.

what

Responses input shape assistant.content = [{text}, {function_call}] was dropping the function_call. The matching function_call_output then orphaned and the conversation died on Missing corresponding tool call for tool response message.

why

_transform_responses_api_input_item_to_chat_completion_message only dispatches by the top-level type. Mixed-content assistant items have role=assistant (no top-level type=function_call), so they fell into the plain-content path and the nested function_call part was thrown away.

fix

In the plain path, detect role==assistant + content is a list containing any type=="function_call" part, and route to a new helper _transform_responses_api_mixed_assistant_content_to_chat_completion_message that splits the list:

  • non-call parts → content (via the existing content transformer)
  • function_call parts → tool_calls

emits ONE assistant message carrying both. The text-only and top-level-function_call paths are untouched.

proof

$ .venv/bin/python -m pytest tests/test_litellm/responses/litellm_completion_transformation/test_responses_mixed_content_function_call.py -v
[...]
test_mixed_content_function_call_emits_tool_calls PASSED
test_mixed_content_pairs_with_function_call_output PASSED
test_text_only_assistant_unchanged PASSED
============================== 3 passed in 0.08s ===============================

sibling tests in tests/test_litellm/responses/litellm_completion_transformation/ (68 pre-existing) still pass.

@CLAassistant

CLAassistant commented May 30, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@codspeed

codspeed Bot commented May 30, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 35 untouched benchmarks


Comparing hclsys:fix/responses-mixed-content-function-call (b220a78) with main (72f1ac4)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (dddbaa8) during the generation of this report, so 72f1ac4 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@codecov

codecov Bot commented May 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@greptile-apps

greptile-apps Bot commented May 30, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

Fixes the silent drop of function_call parts when an assistant message arrives with a mixed content list (text + function_call) in the Responses API input — the downstream function_call_output was becoming orphaned and breaking the conversation.

  • Adds a detection guard in _transform_responses_api_input_item_to_chat_completion_message that checks for role==assistant + a function_call part inside the content list, routing to a new helper instead of the plain-content path.
  • _transform_responses_api_mixed_assistant_content_to_chat_completion_message splits the list: text parts go to content (via the existing content transformer), function_call parts go to tool_calls. The index field is set to the tool-call's position among tool calls (not the raw content-list offset), consistent with the existing top-level function-call handler.
  • Three new mock-only regression tests cover: mixed content promotion, correct function_call_output pairing, and a guard ensuring text-only assistant messages are unaffected.

Confidence Score: 5/5

The change is narrowly scoped to the Responses API input transformation path and does not affect any other code paths.

The fix is well-contained: a single dispatch guard and a new static helper, both mirroring the patterns of the existing function-call transformer. The previously-flagged index offset bug is resolved. Tests are mock-only, cover the exact regression scenario and a non-regression guard for text-only messages, and introduce no network calls.

No files require special attention.

Important Files Changed

Filename Overview
litellm/responses/litellm_completion_transformation/transformation.py Adds mixed-content assistant message detection in the dispatch path, routing to a new helper that correctly splits function_call parts into tool_calls and text parts into content. The previously-flagged index issue is resolved.
tests/test_litellm/responses/litellm_completion_transformation/test_responses_mixed_content_function_call.py New mock-only regression tests covering: mixed content lifts function_call to tool_calls, function_call_output pairs correctly, and text-only assistant messages are unaffected.

Reviews (3): Last reviewed commit: "fix(responses): index tool_calls by tool..." | Re-trigger Greptile

@krrish-berri-2

Copy link
Copy Markdown
Contributor

@hclsys — could you add a screenshot or short video showing the responses API correctly lifting function_call from mixed assistant content into tool_calls (an example request/response or test run output works great)? Visual proof really speeds up review. Thanks!

@hclsys

hclsys commented May 30, 2026

Copy link
Copy Markdown
Contributor Author

@krrish-berri-2 here's the before/after exercised directly against the conversion function:

from litellm.responses.litellm_completion_transformation.transformation import LiteLLMCompletionResponsesConfig

input_items = [
    {"role": "assistant", "content": [
        {"type": "text", "text": "ok"},
        {"type": "function_call", "id": "call_good", "name": "test", "arguments": "{}"},
    ]},
    {"type": "function_call_output", "call_id": "call_good", "output": "result"},
    {"role": "user", "content": [{"type": "input_text", "text": "continue"}]},
]
msgs = LiteLLMCompletionResponsesConfig._transform_response_input_param_to_chat_completion_message(input=input_items)
for m in msgs: print(m.get("role"), "tool_calls=", m.get("tool_calls"), "tool_call_id=", m.get("tool_call_id"))

unpatched main:

assistant tool_calls= None tool_call_id= None      # ← function_call SILENTLY DROPPED
tool      tool_calls= None tool_call_id= call_good  # ← orphan → "Missing corresponding tool call..."
user      tool_calls= None tool_call_id= None

this PR:

assistant tool_calls= [{'id': 'call_good', 'type': 'function', 'function': {'name': 'test', 'arguments': '{}'}}] tool_call_id= None
tool      tool_calls= None tool_call_id= call_good
user      tool_calls= None tool_call_id= None

tool_call_id call_good now pairs with a real tool_calls[0].id, so the downstream Gemini/Anthropic chat path stops raising "Missing corresponding tool call for tool response message".

@hclsys
hclsys changed the base branch from main to litellm_internal_staging May 31, 2026 03:02
@hclsys

hclsys commented Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

@greptileai pushed a fix for the tool_call index — re-review please

@hclsys

hclsys commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

p2 from greptile was addressed in 11c14af — index=len(tool_calls) now indexes by tool-call position (not content-list position), matching the existing _transform_responses_api_function_call_to_chat_completion_message which uses index=0. ready for another look. @greptile-apps please re-review.

@hclsys

hclsys commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

@krrish-berri-2 same env limit as #29501 — can't spin up the proxy from here, but the regression tests exercise the exact mixed-content→tool_calls lift scenario:

$ uv run pytest tests/test_litellm/responses/litellm_completion_transformation/test_responses_mixed_content_function_call.py
3 passed in 0.25s

CI is fully green now. ready for another look.

@hclsys
hclsys force-pushed the fix/responses-mixed-content-function-call branch from 11c14af to 573b361 Compare July 16, 2026 04:06
@hclsys
hclsys force-pushed the fix/responses-mixed-content-function-call branch from 573b361 to c7a4cb1 Compare August 25, 2026 06:05
@hclsys

hclsys commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

rebased onto internal_staging and squashed to one commit. reconciled with the _resolve_file_id signature change that landed on staging meanwhile, and kept the greptile p2 fix (tool_calls indexed by tool-call position via index=len(tool_calls), not the content-list index). the regression test covers the mixed text+function_call round-trip. good for another look.

@hclsys
hclsys force-pushed the fix/responses-mixed-content-function-call branch 3 times, most recently from 22dd0ae to 67019f6 Compare August 26, 2026 19:01
@yuneng-berri
yuneng-berri deleted the branch BerriAI:main September 13, 2026 04:23
@yuneng-berri yuneng-berri reopened this Sep 13, 2026
@devin-ai-integration
devin-ai-integration Bot changed the base branch from litellm_internal_staging to main September 23, 2026 16:08
@devin-ai-integration
devin-ai-integration Bot requested a review from a team September 23, 2026 16:08
…ol_calls

A Responses API assistant message whose content list mixes text/output_text
parts with function_call parts was dropped on the floor when transformed to a
Chat Completion message. Lift the function_call parts into tool_calls (indexed
by tool-call position) while keeping the text content, so the round-trip
preserves both. Adds a focused transform + regression tests.
@hclsys
hclsys force-pushed the fix/responses-mixed-content-function-call branch from 67019f6 to b220a78 Compare October 25, 2026 15:32
@hclsys

hclsys commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

rebased onto main. The responses transformation was refactored since my last push, so I redid the branch on the new code: same fix (function_call parts lifted out of an assistant content list into tool_calls, index by tool-call position), now a small helper plus one branch in _transform_responses_api_input_item_to_chat_completion_message. It goes through the file's _STR_KEY_DICT_ADAPTER / _OBJECT_LIST_ADAPTER and one cast(..) # cast-ok: for the block-list content, so basedpyright's delta gate stays flat. The tests moved under tests/unit/responses/; the tool_calls one fails on main's source, and the rest of that dir still passes (575).

This branch has not been deployed

No deployments
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.

Responses API: function_call not converted to tool_calls in mixed content assistant messages

4 participants