Skip to content

fix(llm): preserve slash-containing message IDs in tool grouping - #8815

Open
remote-controlled-man wants to merge 2 commits into
livekit:mainfrom
remote-controlled-man:codex/prflow-9dcd80eac8fe
Open

remote-controlled-man wants to merge 2 commits into
livekit:mainfrom
remote-controlled-man:codex/prflow-9dcd80eac8fe

Conversation

@remote-controlled-man

Copy link
Copy Markdown

Chat histories with distinct assistant IDs such as conversation/reply/1 and conversation/reply/2 fail with AssertionError: only one message is allowed in a group when converted for any supported LLM provider. The shared grouping helper treated everything before the first slash as the assistant ID.

Keep the complete assistant message ID. For generated function calls, remove only the final /fnc_N suffix when finding the parent; explicit group_id continues to take precedence. This preserves parallel call/result grouping when the assistant ID itself contains slashes.

Validation:

  • On unmodified main, the regression fails for Chat Completions, Responses, Anthropic, Gemini, Bedrock and Mistral conversion.
  • All 17 new regression/control cases pass, including ordinary and slash-containing IDs, explicit groups, single and parallel calls, results returned out of order, and arbitrary legacy call IDs. Other implicit IDs keep the existing grouping convention.
  • make check passed: repository formatting (1,240 files), Ruff and strict mypy (796 source files).
  • uv run --no-sync pytest tests/test_chat_ctx.py tests/test_cache_breakpoint.py tests/test_instructions.py --unit -q -p no:cacheprovider --basetemp=.prflow-tmp/pytest: 218 passed.

The full --unit --audio_eot CI gate was not run locally (livekit-server is not installed). Provider integration and evaluation suites were not run.

No live provider calls were made. Developed with AI assistance and independently reviewed.

@remote-controlled-man
remote-controlled-man requested a review from a team as a code owner November 27, 2026 16:18
Copilot AI balanced review requested due to automatic review settings November 27, 2026 16:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

Comment on lines +166 to +170
parent_id, separator, call_index = item.id.rpartition("/fnc_")
group_id = item.group_id or (
parent_id
if separator and call_index.isdecimal()
else item.id.split("/")[0] # legacy IDs without a generated suffix

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Tool calls on user messages

When a standalone call’s /fnc_N suffix follows a full user message ID, group_tool_calls attaches it to that user message. OpenAI conversion then emits tool_calls on a user message instead of an assistant message.

Learn more

Applications can supply custom item IDs in a chat context, including through from_dict. The inference stream forwards that context to the provider formatter. Non-assistant items and tool groups share the same dictionary, so a parsed parent ID can resolve to a user message. OpenAI conversion keeps that message’s role and adds the tool calls to it, producing an invalid request.

Example: A user message with ID session/query is followed by a standalone call with ID session/query/fnc_0 and its result. Previously, conversion emitted a user message, a separate assistant tool-call message, and the result. It now puts the tool call directly on the user message.

Recommended fix: Give non-assistant items a separate key namespace from assistant messages and tool calls in the same ordered mapping. Keep the full assistant ID, suffix parsing, and explicit group precedence within the assistant/tool namespace. Add session/query/fnc_0 to the existing test for standalone calls next to session/query.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@1AbdullahButt 1AbdullahButt left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes for one narrow case. Assistant messages are now keyed by their full ID, but the legacy fallback still cuts call IDs at the first slash, so a slash-containing reply no longer matches its own calls when they lack /fnc_N.

In Docker (python 3.12, livekit-agents installed with --no-sources), tests/test_chat_ctx.py gave 106 passed on the head, and 8 of the new cases fail on the base. Then I ran to_provider_format("openai") on both:

  • A. A reply conversation/reply/1 with call conversation/reply/1/call_1. The base gives one assistant message with text and tool_calls. The head splits it into a text message and a separate tool-call message.
  • B. Replies conversation and conversation/reply/2, with the call conversation/reply/2/call_1. The base raised the assertion. The head attaches the call to "Hello" and moves "Checking" after the tool result, which silently reorders the turn.

Suggestion: at livekit-agents/livekit/agents/llm/_provider_format/utils.py:170, fall back to the longest assistant-message ID that is a /-prefix of the call ID, and use the first segment only when none matches. I tried that locally: both cases come out right and all 106 tests pass. Please add A and B near tests/test_chat_ctx.py:375.

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.

3 participants