Hi guys I come across an issue with openai.realtim...
# agents
g
Hi guys I come across an issue with openai.realtime.RealtimeModel(model="gpt-realtime") that After calling of function tool is triggered, Livekit agent sometime generate paint message first during processing function tool and then return output of function tool and generate another message based on that output successively. eg.
Copy code
user: "I want to know the weather in Tokyo"
assistant → function call: get_weather("Tokyo")
assistant → "the weather is good" # this is paint message not generated from tool call output
tool → get_weather result: {"temp": 22, "condition": "Sunny"}
assistant → "The weather in Tokyo is 22°C and sunny."
I try to ask chatgpt the reason for this behavior, it replies
Copy code
While your tool is still processing, the model can still continue generating messages — for example:
System messages like "Let me check that for you…"
Clarification or reasoning text (if streaming is on)
Partial outputs, if the LLM is continuing a response stream
This is because LiveKit Agents are concurrent — the function tool runs in one task, while the model's conversation stream remains open in another.
Can you guys confirm whether the answer is valid or not? If valid, then it should be a very serious bug in livekit agent right? since we expect livekit agent performa as below: eg.
Copy code
user: "I want to know the weather in Tokyo"
assistant → function call: get_weather("Tokyo")
tool → get_weather result: {"temp": 22, "condition": "Sunny"}
assistant → "The weather in Tokyo is 22°C and sunny."
l
If a model has a tool, it shouldn't also be trying to answer the question itself and duplicating the output. I'm not sure how you are testing the tool calls, but I tried adding a simple get_weather tool call to https://github.com/livekit-examples/python-agents-examples/blob/main/realtime/openai-realtime-tools.py , with the
gpt-realtime
model, and the only response comes from the tool output. How are you getting the duplicate responses?
g
@lively-winter-88668 Since this problem occasionally happens. Even though we give you source code or log, you may not reproduce it. However, I can share our Livekit agent model setting for you
Copy code
Agent(llm=openai.realtime.RealtimeModel(
                model="gpt-realtime",
                voice="marin",
                turn_detection= SemanticVad(
                    type="semantic_vad",
                    create_response=True,
                    interrupt_response=True,
                    eagerness="low",
                    )
                temperature=0.6,
                input_audio_noise_reduction="far_field",
                input_audio_transcription=AudioTranscription(language=lang, model="whisper-1"),
                max_session_duration=20 * 60,
            ))
with you
@lively-winter-88668 BTW, other livekit agent user also has similar problem https://livekit-users.slack.com/archives/C088ZNU7QQ5/p1761749885041149?thread_ts=1761729896.570059&cid=C088ZNU7QQ5 I hope you can investigate it.
@lively-winter-88668 Do you have any update for this case?
l
I don't believe the issue is the same? https://livekit-users.slack.com/archives/C07FY8WHGPM/p1761803770776449, that user was calling tools from within
on_user_turn_completed
g
@lively-winter-88668 We still come across the issue sometime now. We find that the issue may be related to the result version of livekit for this PR https://github.com/livekit/agents/pull/3511 The update somehow contradict to your explanation for the expected behavior of function call of openai realtime model. Can you clarify what the update in that PR means?
l
My reading of that pull request is that if the agent is currently talking, do NOT interrupt it by sending the result of the tool call, so wait for the model to finish replying before sending the tool results.
g
@lively-winter-88668 I find that it may be due to openai api. Some of the developers report similar problem for native openai api https://github.com/openai/openai-agents-js/issues/180#issuecomment-3073088481
@lively-winter-88668 is it possible that the PR https://github.com/livekit/agents/pull/3511 make agent send the tool result after the last pure generated message? Before the PR, the last pure generated message is interrupted by sending the result of the tool call and thus we don' t have this issue before? I have this suspicious because we seldom come across this issue before 10/29 (the PR take effect since 10/28) 🤔
@lively-winter-88668 Also, in your trial for adding a simple get_weather tool call to https://github.com/livekit-examples/python-agents-examples/blob/main/realtime/openai-realtime-tools.py , with the
gpt-realtime
model, can you add long sleep time inside the function tool? I suspect it happens when long time processing in function call
l
😕 I had another go at reproducing this, with https://github.com/livekit-examples/python-agents-examples/blob/main/realtime/openai-realtime-tools.py , and changing the model to
gpt-realtime
. Added a 10 second delay to the temperature conversion function call,
await asyncio.sleep(10)
, and repeatedly asked for temperature conversions. Consistently the agent would wait 10 seconds, then respond to my question - what behaviour do you see differently?
g
@lively-winter-88668 This problem still occasionally happen. can you reply my question here? https://livekit-users.slack.com/archives/C07FY8WHGPM/p1762428989463269?thread_ts=1761729135.359359&cid=C07FY8WHGPM
@lively-winter-88668 I can successfully reproduce the problem with minimal example. The code is attached. Please have a look. Steps to reproduce it: 1. "uv run agent.py dev" 2. go to livekit agent playground (https://agents-playground.livekit.io/) to connect to agent 3. after agent greet, say "thank you" to her 4. after agent ask your name, say "can you tell me the weather in Tokyo today?" 5 If she haven't replied you with the answer, please keep ask "can you tell me the weather in Tokyo today?".
@lively-winter-88668 The agent will reply pure message and call function tool at the same time. The recording for the example is shown here: https://www.loom.com/share/a02003b564964519a35821d8f119b1a2 Sine the problem may occasionally happens, please retry the whole steps above if the problem hasn't been shown.
l
Clearer! I am now able to reproduce the behaviour shown in the video. It looks like this is caused because time.sleep does not yield control back to the event loop, which pauses speech. If I replace this with
asyncio.sleep(10)
the speech continues but the weather is not returned for the required 10 seconds. For a real weather API call, an async HTTP client could be used.
👍 1
c
@lively-winter-88668 Thanks for helping investigate this I am on the same team as @great-gpu-99842 - but I wanted to explain a little bit more here In the example shared above, the behaviour we think we are seeing is something like
Copy code
user: "I want to know the weather in Tokyo"
assistant → function call: get_weather("Tokyo")
PARALLEL: assistant → "Let me tell you the weather" # this message not generated from tool call output
PARALLEL: tool → get_weather result: {"temp": 22, "condition": "Sunny"}
assistant → "The weather in Tokyo is 22°C and sunny. "
But what maybe a lot of developers might want in this situation is
Copy code
user: "I want to know the weather in Tokyo"
assistant → function call: get_weather("Tokyo")
tool → get_weather result: {"temp": 22, "condition": "Sunny"}
assistant → "The weather in Tokyo is 22°C and sunny."
Now that's not to say that all developers would want that, but in our use case, its definately desirable, as in our business case we are seeing sometimes something less user friendly like
Copy code
user: "I want to know the weather in Tokyo"
assistant → function call: get_weather("Tokyo")
PARALLEL: assistant → "I do not know the weather."
PARALLEL: tool → get_weather result: {"temp": 22, "condition": "Sunny"}
assistant → "The weather in Tokyo is 22°C and sunny. "
so we definately would prefer the model to not say anything until tool calls are complete. Whether the agent pauses or not, as you say, likely comes down to whether the tool call is blocking the asycio loop or not, but that is not our main concern. What we are wondering is if there is a way to ensure that order of operation is tool call --> message My understanding of the underlying OpenAI Realtime model is that it can generate both messages and tool calls simultanously, and as far as I can see they expose no options to constraint it to prioritise tool calls, so as far as I can see we can either • Set the code to interrupt the speech whlilst tool call is generating
Copy code
user: "I want to know the weather in Tokyo"
assistant → function call: get_weather("Tokyo")
PARALLEL: assistant → "Let me-" # INTERRUPT via session.interrupt
PARALLEL: tool → get_weather result: {"temp": 22, "condition": "Sunny"}
assistant → "The weather in Tokyo is 22°C and sunny. "
• Or try and prompt the realtime API "Please always call the get_weather() without generating a speech message simulatnously - although I have no idea if that would work I also just wanted to confirm that indeed the intention is that we wait for messages to complete whilst in background performing tool calls? The reason we raise this to the livekit team rather than simply raising a PR internally, is that it looks to me like this issue has got more common recently for us- we assume due to https://github.com/livekit/agents/pull/3511 - as it seems like that PR removes the INTERRUPT in favour of
await
for the initial message to complete - does that agree with your understanding? I think this is a valid design choice, but it seems to me that depending on the use case, developers may prefer the old behaviour - so having this as an option ("await_message_completion_during_tool_call: bool") might be favourable?
It seems @narrow-chef-71655 is encountering a similar issue - so I can be confident this is not only an undesirable behaviour to our own team https://livekit-users.slack.com/archives/C07FY8WHGPM/p1761803770776449 The common issue here is that we desire the tool call to complete before a response is generated, not simultaneously
cc @jolly-air-155 who worked on https://github.com/livekit/agents/pull/3511 - you probably have the clearest understanding here I suspect
We log the underlying OpenAI
response.output_item.added"
events for clarity - Terry shared them with me so I attach them here In the example Terry shared the pure message "of course! let me" and the function call for "lookup_weather" are added consecutively before any further livekit logs occur - which at least confirms our suspicion that the openai model is creating both message and tool call at once (and then livekit is awaiting the former message to finish even while a new message from tool call is ready)
Copy code
2025-11-17 11:00:00,630 - DEBUG __mp_main__ - Response output item added: type=message {"item_type": "message", "item_id": "item_Ccip6vtfzDLWeDwOEHibO", "response_id": "resp_Ccip6N6o5erKW1FbqGMJ3", "pid": 15830, "job_id": "AJ_SjrGpqeDd6ry"}
2025-11-17 11:00:01,652 - DEBUG __mp_main__ - Response output item added: type=function_call {"item_type": "function_call", "item_id":
l
Hi Benjamin, apologies for not replying on Friday. I hadn't appreciated that the underlying issue was a desire to ignore any message prior to the tool calls, neither did I appreciate that these messages were detrimental to the user experience beyond the time taken for them to play out. Based on your explanation, I agree that workarounds that involve interrupting the LLM or instructing it to not respond before tool calls are unlikely to be reliable. An additional option,
await_message_completion_during_tool_call
does sound like a good idea to me, based on your explanation but I'm not in a position to say whether it is practical from an implementation standpoint. I recommend raising an issue in the agent repo, flagging that this is a regression caused by https://github.com/livekit/agents/pull/3511 (maybe 'regression' is the wrong word, but you know what I mean). At this point, it feels like it needs the eyes of the engineering team but although they often do respond in this community forum, obviously they have a lot of asks on their time.
c
OK thanks, I will raise an issue there