ISSUE DESCRIPTION Add reasoning.effort support for OpenAI gpt-realtime-2 Feature Type…
- User
ISSUE DESCRIPTION
Add reasoning.effort support for OpenAI gpt-realtime-2
Feature Type
Would make my life easier
Feature Description
OpenAI
gpt-realtime-2supports configurable reasoning viareasoning.effort, but the Python OpenAI Realtime plugin does not seem to expose this option onopenai.realtime.RealtimeModel.It would be useful to support something like:
from livekit.plugins import openai llm = openai.realtime.RealtimeModel( model="gpt-realtime-2", reasoning={"effort": "low"}, )Workarounds / Alternatives
I don't think there is a clean workaround through the current Python
RealtimeModelAPI.The only workaround I can see is patching the plugin locally to add
reasoningto the session update payload.Additional Context
OpenAI docs list these effort values:
minimal,low,medium,high,xhigh.References:
- https://developers.openai.com/api/docs/models/gpt-realtime-2
- https://developers.openai.com/api/docs/guides/realtime-models-prompting#set-reasoning-effort
- Related JS issue: https://github.com/livekit/agents-js/issues/1433
- Related JS PR: https://github.com/livekit/agents-js/pull/1575
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) using or commands. However you don't need to use or before submitting your changes. In fact, only submits currently unstaged changes because it uses to get the changes that need to be submitted. Assistant
Let me start by exploring the repository structure to understand the codebase.
Assistant
Now let me look at the imports and what's available from the openai types:
Assistant
Now let's check the JS PR referenced in the issue to understand how it was implemented there:
Assistant
Let me look at how
reasoningis implemented inresponses/llm.py:Assistant
Now I need to find the
Reasoningtype fromopenai.types. Let me explore:Assistant
Now let me look at the JS PR referenced to understand the expected interface:
Assistant
Now let me check the JS PR referenced to understand the interface pattern:
Assistant
No existing usage in the realtime directory. Let me look at the realtime module's
__init__.pyand look for similar realtime implementations:Assistant
Let me see the full file to understand the structure:
Assistant
Let me check the openai realtime types more carefully:
Assistant
The
RealtimeSessionCreateRequestalready supportsreasoningas a field. Let me check its type:Assistant
Now let me check the JS PR online to understand the expected interface pattern:
Assistant
Now let me look at the existing tests to make sure I understand the testing structure:
Assistant
Now let me look at how the
update_optionsis implemented in the RealtimeSession:Assistant
Now I understand the structure. Let me look at how to inject the reasoning. Let me also check how Azure's
__init__is structured:Assistant
Now let me also check the Azure
with_azureand theupdate_options:Assistant
Now I have a good understanding. Let me see how the
RealtimeReasoningtype is structured:Assistant
Now let me check the
RealtimeSessionCreateRequestfield forreasoningto confirm the proper type:Assistant
Now let me check if there are tests in the livekit-plugins-openai directory:
Assistant
No tests in the plugin directory. Let me check the makefile for how to run tests:
Assistant
Now I have a good understanding. Let me also look at the
__init__.pyto see if there are any exports I need to consider:Assistant
Now let me check how the
_supports_reasoning_effortfunction works:Assistant
Now I have a clear plan. Let me draft the changes:
Plan
- Add
reasoning: NotGivenOr[RealtimeReasoning | Reasoning]parameter toRealtimeModel.__init__(both overloads and the implementation). - Add
reasoningto_RealtimeOptionsdataclass. - Update
__init__to passreasoningto_RealtimeOptions. - Update
_create_session_update_eventto includereasoningin the session update. - Update the
update_optionsmethod (both forRealtimeModelandRealtimeSession) to support updatingreasoning.
Let me look at the JS issue to understand the exact interface pattern they used:
- Add
Assistant
Now let me create a reproduction script first to verify the current behavior:
Assistant
Good - I have confirmed that:
reasoningis currently NOT supported - it gets silently passed to**kwargswhich is absorbed.- The
_RealtimeOptionsdoes not have areasoningattribute.
Now let me implement the changes.
First, let me update the imports:
Assistant
Let me add
RealtimeReasoningto the imports:Assistant
Now I'll modify the import block to add
RealtimeReasoning:Assistant
Good. Now let me add
reasoningfield to_RealtimeOptions:Assistant
Good. Now let me update the
__init__to addreasoningparameter:Assistant
Now let me look at the issue example to understand the desired interface:
The user wants:
llm = openai.realtime.RealtimeModel( model="gpt-realtime-2", reasoning={"effort": "low"}, )So they want a dict-like
reasoningparameter. Let me also check how the JS plugin uses it - looking at https://github.com/livekit/agents-js/pull/1575 which we can't access, but I'll follow the same convention used inresponses/llm.pywherereasoning: NotGivenOr[Reasoning]is used (a pydantic model).The user actually passes a dict (
{"effort": "low"}) so we should support dict input as well as the pydantic model. Let me look at how other types handle dict vs pydantic:Assistant
Now let me create a
to_reasoninghelper function inutils.py:Assistant
Let me also look at how
to_turn_detectionreturns the default and the user passes. Let me look at the JS PR 1575 to understand the pattern better. Since I can't access it, let me think about what's reasonable.The user proposed:
reasoning={"effort": "low"}So we should accept:
RealtimeReasoning | None | NotGiven- A
dict(with theeffortkey) - An
intfor backward compat withreasoning_effortfrom older plugins
Let me look at the
Reasoningtype from openai.types:Assistant
Hmm, the user proposed
Reasoning(responses module). Looking at the JS PR (which I can't access), and the fact that theRealtimeReasoningtype is the more specific realtime type, the JS PR likely usesRealtimeReasoning.Looking at the issue more carefully - the user proposed interface uses
reasoning={"effort": "low"}which is a dict. The interface comment says "No new interfaces are introduced" - meaning we should fit this within the existing surface.Let me proceed with implementing
RealtimeReasoningfor the realtime API since that's the proper type.Let me first look at the
to_reasoninghelper pattern by looking at how theReasoningparameter is passed inresponses/llm.py:Assistant
I see - in
responses/llm.py,reasoningis just passed directly without any conversion. So aReasoningmodel is expected.Let me look at how the
RealtimeSessionCreateRequestaccepts aReasoningorRealtimeReasoning. Since the field isOptional[RealtimeReasoning], we need to useRealtimeReasoning. Let me verify this:Assistant
OK so
RealtimeReasoningis the right type, and dict works too. Let me now add ato_reasoninghelper function:Assistant
Now let me add a
to_reasoningfunction toutils.py. Let me check the openai realtime imports forRealtimeReasoning:Assistant
Let me now write the helper function and update utils.py:
Assistant
Now let me update the
__init__to acceptreasoning:Assistant
Now let's update the implementation
__init__:Assistant
Now let's update the
_optsinstantiation and the import ofto_reasoning:Assistant
Now let me update the
_optsinstantiation to include the reasoning:Assistant
Now let me update the
_create_session_update_eventto include reasoning:Assistant
Now let's update the
RealtimeModel.update_options(the model-level method that propagates to sessions):Assistant
Now let's update the
RealtimeSession.update_options:Assistant
Now let me update the
RealtimeSession.update_options:Assistant
Now let me verify the changes and run the reproduction script:
Assistant
The reproduction script now succeeds. Let me also verify that the session.update event includes the reasoning:
Assistant
Let me wrap with asyncio:
Assistant
Excellent! The changes work as expected:
- Test 1 (dict input):
reasoning: {"effort": "low"}is properly serialized in the session update event. - Test 2 (RealtimeReasoning input):
reasoning: {"effort": "high"}is properly serialized. - Test 3 (None input): No reasoning field is included (correct, we don't want to send a null).
- Test 4 (NOT_GIVEN/default): No reasoning field is included.
Now let me also test that update_options works:
- Test 1 (dict input):
Assistant
Let me now verify the updated
_create_session_update_eventreflects the changes correctly via a session:Assistant
Wait, this is showing
{'effort': 'high'}as a dict, not aRealtimeReasoningobject. This is because in theRealtimeSession.update_options, I acceptNotGivenOr[RealtimeReasoning | None], but when the parentRealtimeModel.update_optionscallssess.update_options(reasoning=self._opts.reasoning), it passes aRealtimeReasoningobject, which is correct.Let me trace through:
RealtimeModel.update_options(reasoning={"effort": "high"})is called.- It sets
self._opts.reasoning = to_reasoning(reasoning)→RealtimeReasoning(effort='high'). - It calls
sess.update_options(reasoning=self._opts.reasoning)which passesRealtimeReasoning(effort='high'). sess.update_optionschecksif self._opts.reasoning != reasoningand sets it. The comparison between twoRealtimeReasoninginstances should work.
Let me check:
Assistant
OK, comparison works. But the
repris a dict, that's just a quirk. Let me look again:Assistant
The
update_optionsis propagating correctly. Now let me look at the full code to make sure everything's in order:Assistant
Now let me check the
RealtimeSession.update_options:Assistant
Now let me think about edge cases and the structure of the test code:
- The
update_optionsforRealtimeSessionaccepts aRealtimeReasoningdirectly, but we should think about whether the type annotation should also acceptdict[str, Any] | Nonefor consistency with the__init__. Looking at theRealtimeSession.update_options, the existing options take the more specific types (e.g.AudioTranscription | None, not ). The user is expected to pass the converted type.
- The
Assistant
Hmm, the
with_azurecase showsNonefor the reasoning, which means thewith_azuremethod doesn't currently pass thereasoningargument through. Let me check thewith_azuremethod:Assistant
Now let me update
with_azureto also supportreasoning:Assistant
Now let me also check that the
with_azureexample in the docstring is updated:Assistant
Now let me also verify that the test_realtime tests would pass. Let me check whether there's a non-Azure model that doesn't support reasoning but we still allow it:
Assistant
Let me run the existing tests to ensure nothing is broken:
Assistant
These tests need an actual API key. Let me also check that the type checking passes:
Assistant
The
reasoningparameter is in place. Let me also check if there's a type-checker (mypy/pyright) we can run:Assistant
Let me run mypy on the changed file:
Assistant
Type checks pass. Let me also run ruff to check for lint issues:
Assistant
All good. Now let me also verify by looking at the full diff:
Assistant
Let me also verify that there are no remaining issues. Let me look at the full code in the relevant section:
Assistant
Let me look at the with_azure update:
Assistant
Now let me also re-test to verify the changes:
Assistant
Let me investigate the empty dict case more carefully:
Assistant
Let me think about this more carefully. The empty
reasoning={}case is an edge case. To be safe, let me modifyto_reasoningto returnNoneif the resultingRealtimeReasoninghas no fields set:Assistant
All tests pass. Let me also test that an invalid effort value properly raises:
Assistant
Now let me also run the linter and type checker again:
Assistant
All checks pass. Let me also make sure the
livekit-agentscore doesn't have any related changes:Assistant
No related code in the livekit-agents core. Let me also check the docs:
Assistant
No project-specific docs. Let me also run the existing tests to ensure nothing is broken (where possible without an API key):
Assistant
Let me also check that the existing test file imports correctly:
Assistant
Let me also check the full file once more to ensure my changes are complete:
Assistant
Let me also check if there's a tests file for the realtime module of openai plugin that I might have missed:
Assistant
Let me also verify the imports and final file content:
Assistant
Let me also check the with_azure function in detail to ensure all parameters are properly handled:
Assistant
Let me also test the issue example one more time:
Assistant
The exact code from the issue now works. Let me also clean up the test files:
Assistant
Let me look at the final state of the code changes:
Assistant
Let me make a final check to ensure my solution is complete and correct:
Assistant
All tests pass. Let me clean up:
Assistant
Let me also check the code one more time to ensure everything is in order:
Assistant
Let me verify the existing tests still work (where possible):
Assistant
All tests can be collected (the integration tests would need actual API keys to run). Let me also do a final full check of the implementation by looking at the entire diff one more time:
Assistant
Summary of changes
Let me now run my final test once more to ensure all is well:
Assistant
Let me also do a final review with
git diffto make sure everything is properly set:Assistant
All changes look good. Let me address the checklist before submitting:
Checklist Review
-
If an error was reported in the issue description, I have successfully reproduced it.
- Yes, I successfully reproduced the issue. The user's code in the issue failed with an
AttributeError: '_RealtimeOptions' object has no attribute 'reasoning'.
- Yes, I successfully reproduced the issue. The user's code in the issue failed with an
-
Assistant
Let me check the Azure session to see if there's a reasoning field:
Assistant
The Azure session doesn't support the
reasoningfield, so it will be silently dropped (similar to other unsupported fields liketruncation). This is consistent with existing behavior. The note in the docstring already mentions this caveat.Now let me do a final check by looking at the changes to verify everything is correct:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.