← Back to Pull requests

Let the Realtime conversation continue while client tool calls are pending #772

Closed
omkar-334 closed this pull request without merging on Nov 18, 2026 11:24 UTC
View on GitHub
omkar-334 omkar-334 commented on Oct 30, 2026 10:24 UTC
Author

Description


I removed the guards that rejected response.create and local model generation while a client function call had no output yet. The conversation can now continue on an unrelated turn while a slow tool is running, and the result is accepted whenever it arrives.

Fixes #760.

Brief

Dropping the guard alone is not enough, and I hit both problems when I tried it:

1. A call without its output is invalid for most providers, so a pending call cannot go into the model request.
2. Once the call is left out, the model no longer knows the tool is running. It calls it again. Without any hint, the tool was re-issued in 47 of 48 runs across 6 OpenAI models and both backends.

So a pending call is left out of the request, and the request ends with a short system note naming the tools that are still running (name and arguments only, no call ids). It tells the model not to call them again, not to guess their results, and to answer the latest message even if it is about something else. The note exists only on the request and never enters the stored history.

I tried it in the system prompt first. gpt-4.1-mini still re-issued the call in 4 of 6 runs there, and 0 of 6 when the note is the last message, so it goes last.

Changes

  • api/openai_realtime/handlers/response.py: remove the function_call_output_pending admission check.

  • LLM/language_model.py and LLM/base_openai_compatible_language_model.py: remove the three "Cannot generate a response while function call outputs are pending" guards.

  • LLM/chat.py:

  • - Chat.pending_tool_calls_notice() builds the note.
    - A second function_call_output for the same call_id is now an error. Results are accepted once, in any order.
    - A result that arrives after a later user turn was already answered is serialized where it arrived (its call and any reasoning prefix move with it). Otherwise it stays next to its call, as before. Without this the next response repeated an answer.
    - When one parallel call is pending and another is finished, the finished call and its output are kept.
  • chat_completions_language_model.py, responses_api_language_model.py, language_model.py: append the note as the last message.

  • Fallback: some chat templates (Mistral, Gemma) and providers reject a system message that is not first. If a provider returns a 400 while the note is present, the handler retries once with the note folded into the last user message and stays in that mode. A local template that refuses the note is detected before generation and handled the same way.

  • api/openai_realtime/README.md: new "Pending tool calls" section.
  • Testing

    Unit tests: tests/openai_realtime/test_pending_tool_calls.py (continued conversation, multiple pending calls resolved out of order, late result after an unrelated turn, duplicate result, result arriving mid response, cancel, failed response, barge-in, prefetch waiting for the last call, a websocket reproduction of the sequence in the issue) and tests/test_pending_notice_fallback.py. The existing tests that asserted the old rejection were updated. I checked that the first ten of these fail when the old guards are put back. Full suite, ruff and mypy pass.

    Live checks, not included in the PR:

  • 5 OpenAI models (gpt-4.1-mini, gpt-4.1, gpt-4o, gpt-5-mini, gpt-5.4-mini) on both backends, 4 runs each: 0 of 40 re-issued the call, 40 of 40 answered the unrelated question, 40 of 40 used the late result.

  • End to end on a real server with OpenAI STT and TTS, spoken input, single pending call and two parallel calls with one result and a follow up while the other is pending: 6 of 6 steps for each of chat-completions gpt-4.1, responses-api gpt-4.1 and responses-api gpt-5.4-mini. gpt-4.1 on the Responses backend ran with reasoning effort omitted (see #771).

  • The same flow with Fish S2.1 Pro on OpenRouter for TTS: 6 of 6.

  • Local Qwen2.5 1.5B and 3B (4-bit): no re-issue, the new question answered, and the late result used when the history is realistic.

  • A non-OpenAI model, nvidia/nemotron-3.5-lightning on OpenRouter: 8 of 8.
  • Note

  • The note is guidance, not enforcement. cohere/north-mini-code on OpenRouter re-issued the call in 5 of 6 runs with the note (6 of 6 without it). Folding the note into the last user message cut that to 2 of 6 for that model, but made gpt-4o on the Responses API repeat the call in 3 of 4 runs, so the system message stays the default. I did not add a setting for the placement.

  • The late result ordering change touches how history is serialized, so that is the part I would look at most closely.

  • A duplicate function_call_output used to be accepted silently and is now an error.

  • Follow up prefetch still waits until no call is pending. That is deliberate.

  • A pending call that is cancelled or fails stays pending until its output arrives. The README says so.

  • gpt-audio models reject the existing text only warmup. That was already the case and I did not touch it.
  • Diff: 915 lines (+806 / -109) across 17 files Reviewer: None requested Review duration: 27420m
    All Vouch review confidence checks passed
    Residual risk 0.25 = Change Risk (0.89) × (1 − Review Confidence (0.71)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.89 · 915 lines diff across 17 files
    0.89
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.51 · 2 substantive comment(s)
    0.51
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 10%
    71%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.400
    No Reviewer Assigned no_reviewer_assigned
    0.120
    Mass File Touch mass_file_touch
    0.080
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    3 items
    omkar-334 omkar-334 commented on Oct 30, 2026 10:26 UTC
    Author

    Checklist against the expected behavior in #760. 5 of 6 are met. One is met with a caveat.

  • Pending call identity kept, each result accepted once, out of order included. Results are matched by call_id. A second output for the same call is now an error. Covered by test_multiple_pending_calls_resolve_out_of_order and test_duplicate_result_for_one_call_is_rejected.

  • Multiple pending tools, not treated as an active response. Pending calls live in the chat, not in the response state. Covered by the two call tests above and by the end to end two scenario (two parallel calls, one result, a follow up while the other is still pending), which passed on chat-completions gpt-4.1, responses-api gpt-4.1 and responses-api gpt-5.4-mini.

  • Response ordering, barge-in, cancellation and history consistency. Cancel, a failed response and server VAD barge-in each leave the pending call and its later result intact (one parametrized test). A result that arrives after a later answered turn is placed where it arrived, so the next response answers it. Barge-in is covered at the service level only, not with live microphone audio.

  • No invented results, no duplicate follow-ups. Partly. The server never invents a result, and a result arriving during another turn is deferred until that response ends and starts no follow-up by itself (tested). The model is told not to call a running tool again, but that is guidance. Across 5 OpenAI models and both backends it held in 40 of 40 runs, and it held on a non-OpenAI model (Nemotron, 8 of 8). cohere/north-mini-code still repeated the call in 5 of 6 runs.

  • Backend capability handling. A pending call is never sent to any provider (Transformers, MLX, Responses, Chat Completions), so no request has an unpaired call. If a provider or chat template rejects the trailing system note, the note is folded into the last user message, and the behavior is documented in the README. I checked the template side against a real strict template, Mistral 7B Instruct v0.3 (tokenizer files only): it rejects the note with TemplateError: Only user and assistant roles are supported, with the exception of an initial optional system message!, the helper folds it, and the rendered prompt contains the note. The API retry path (a provider answering 400) has only run against fake providers so far. The handler stays in folded mode only after the retry succeeds, so an unrelated 400 does not change it.

  • Regression coverage. Continued conversation before completion, multiple pending calls, late results, interruption (cancel, failure, barge-in), failed tool output resolving only its own call, and follow up prefetch waiting for the last pending call. A tool callback that raises was already covered by the existing audio client tests, which return an error output for that call.
  • Not covered by the issue but worth knowing: placing the note as a system message is a design choice. Folding it into the user message helped the Cohere model (2 of 6 repeats) but made gpt-4o on the Responses API repeat the call in 3 of 4 runs, so I kept the system message.

    andimarafioti andimarafioti commented on Nov 18, 2026 11:24 UTC
    Contributor

    Addressed in #829, thank you for your help here!
    <img src="https://static.klipy.com/ii/35ccce3d852f7995dd2da910f2abd795/c4/c9/2s2mPpE5.gif"/>

    Reviewers
    None requested
    Assignees
    omkar-334 omkar-334
    Labels
    risk: low confidence: 71.3% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 88.5% (HIGH) · Higher = larger blast radius
    88.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 71.3% · Lower = rushed or shallow review
    71.3%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 25.4% · Safe to merge
    25.4%