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 what we're working with.
Assistant
Let me look at the entire file. The file is large. Let me search for specific parts related to the options.
Assistant
Let me look at the constructor and the relevant options section:
Assistant
Let me continue reading:
Assistant
Now let me understand the OpenAI types and what the
reasoningfield looks like:Assistant
Now let me check the JS PR to understand how it was implemented:
Assistant
Now let me look at the related models.py file to see the realtime models:
Assistant
Now let me look at the existing tests:
Assistant
Now let me look at the existing tests more and the rest of the code:
Assistant
The type accepts a
dictforreasoning. Let me also check the type for theeffortfield:Assistant
Now let me look at the JS PR for reference:
Assistant
Now let me look at the openai.types.realtime.realtime_reasoning module:
Assistant
Now let me look at the existing tests structure:
Assistant
Let me also check the Azure session types and the existing approach to truncation in
_wrap_session_update:Assistant
Now let me check the imports and the existing handling of truncation to follow the same pattern:
Assistant
Now I have a clear plan. Let me start implementing:
Assistant
Now let's start making changes. First, let's add the
RealtimeReasoningimport:Assistant
Now let me add the
RealtimeReasoningimport afterRealtimeTruncation:Assistant
Now let me update the
_RealtimeOptionsdataclass to add areasoningfield:Assistant
Let me try a smaller, more specific edit. I'll add the
reasoningfield to the _RealtimeOptions dataclass.Assistant
Let me look at the file with sed and check more carefully:
Assistant
Let me try a different approach with sed:
Assistant
Now let me update the constructor (the first
__init__overload, the second__init__overload, and the actual__init__method):Assistant
Now let me update the second overload (Azure) too:
Assistant
Now the actual
__init__method:Assistant
Now let me find the docstring and add the parameter documentation:
Assistant
Now let me find the place where
_RealtimeOptionsis initialized with the actual values:Assistant
Now let me update the
with_azureclassmethod:Assistant
Let me check how the Azure session conversion excludes unknown fields:
Assistant
That's a problem - Azure is silently accepting the
reasoningfield but it wouldn't actually do anything with it. Let me see the actual current_oai_session_to_azurefunction:Looking at the existing
_oai_session_to_azurefunction, only specific fields are explicitly mapped (modalities,voice, etc.). The reasonreasoningis leaking through is because the function doesn't explicitly exclude it. Let me think about this.Wait, the model_construct on AzureSession does a strict validation in Pydantic. If
reasoningis unknown field, it should be rejected... But it didn't reject it above. Let me test with the actual AzureSession model_validate:Assistant
Azure is accepting the
reasoningfield without complaining. This is becauseAzureSessionhasextra = "allow"set, OR thetype: "realtime"is treated literally. Either way, we need to make sure that for Azure mode, we don't include the reasoning field.Let me check the AzureSession class to confirm:
Assistant
Let me verify by checking the existing
_oai_session_to_azurefunction:Assistant
The
_oai_session_to_azurefunction only explicitly adds fields, so unknown fields likereasoningare not included in the output. So we don't need to worry about reasoning leaking to Azure.Now let me handle the case where the user wants to pass a dict (like the issue suggests:
reasoning={"effort": "low"}). Let me check the current typing more:Assistant
Let me see how it looks after the conversion to dict with truncation:
Assistant
Now let me update the
with_azuremethod (constructor and call):Assistant
Now let me find the docstring of
with_azureand add the parameter documentation:Assistant
Now let me find the actual
RealtimeModel(...)constructor call insidewith_azureto pass the reasoning:Assistant
Now let me update the
update_optionsmethod onRealtimeModel:Assistant
Now let me update the
_create_session_update_eventmethod:Assistant
Now let me update the
update_optionsmethod onRealtimeSession:Assistant
Let me look at the full
update_optionsto see the existing pattern:Assistant
Now let me verify everything imports correctly:
Assistant
Now let me write a script to test the changes:
Assistant
The dict form is stored as dict. Let me normalize it for consistency by using
RealtimeReasoningtype consistently. Let me check that the dict form actually works when creating the session update event:Assistant
Let me normalize internally to
RealtimeReasoningto avoid the warning:Assistant
The placement looks a bit off - it's spilling onto multiple lines. Let me check if this is syntactically correct. Actually, Python's implicit line continuation should handle this OK. Let me check the indentation.
Actually, looking at the structure again, the value is being assigned to
reasoning=, but my replacement put the whole expression afterreasoning=with parens and indentation. Python can handle this with implicit string concatenation (no parens needed within the actual assignment) but it requires the next line to start at the same indent as the start. Let me verify this works.Assistant
The conversion is working. Now let me also fix the formatting to be cleaner:
Assistant
Now let me also update the type signature to accept both
RealtimeReasoning | dict | None:Assistant
Now let me run a more complete test:
Assistant
Excellent! Now let me also test that the full session update payload is correct:
Assistant
Now let me also test the
update_optionsmethod on the model:Assistant
Now let me look at the test results to see if my reasoning update properly tracks dicts:
Assistant
The dict is stored as dict in
update_options. Let me normalize consistently everywhere:Assistant
Let me think about this more carefully. The simpler approach is to NOT normalize in the constructor, and just always pass the dict to Pydantic. Pydantic will validate it. But this leads to inconsistency. Let me normalize in all three places consistently:
Assistant
Now let me verify this all works:
Assistant
All tests pass. Let me also verify the Azure path:
Assistant
The Azure path correctly excludes the reasoning field. Now let me check the tests:
Assistant
Now let me also check the
__init__.pyof the realtime module:Assistant
Let me also check if there's anything I should be exporting from the realtime model:
Assistant
Let me also look at the complete updated file to ensure everything looks good:
Assistant
Everything looks good. Now let me also see the docstring in with_azure as my replacement may not have aligned with the existing patterns properly:
Assistant
Let me update the docstrings to match the actual types:
Assistant
Now let me run the full test once more:
Assistant
All tests pass. Now let me also verify the module imports cleanly without breaking anything:
Assistant
Let me also look at if the type signature is correctly visible (i.e., IDE auto-completion support):
Assistant
Now let me also check if there's anything I should add for the
update_optionsdocstrings:Assistant
Let me also look at the final overall structure to make sure everything is consistent:
Assistant
Let me also clean up the constructor's argument formatting (which has the multi-line reasoning with parens):
Assistant
The formatting looks a bit awkward. Let me improve it. I'll create a helper function for clarity:
Assistant
Let me add a
_normalize_reasoninghelper function:Assistant
Now let me update the three places that normalize reasoning to use the helper function:
Assistant
Let me verify by running the tests again:
Assistant
Excellent! All tests pass. Let me view the key parts of the file to make sure everything looks good:
Assistant
Wait, I notice on line 731, when
update_optionson the session is called, we pass the originalreasoning(which could be a dict or NOT_GIVEN). But on the session side, we normalize it again with_normalize_reasoning. Let me check if the_normalize_reasoningis being called both on the model and the session for the same call:RealtimeModel.update_options(reasoning=...):- Normalizes and stores in
self._opts.reasoning. - Calls each session's
update_options(reasoning=...)with the originalreasoningarg. - The session's
update_optionsthen normalizes with_normalize_reasoning.
- Normalizes and stores in
Actually, that's a bit redundant. The model already normalizes it once for its
_opts, and then passes the original (un-normalized) to each session, which normalizes again. This works but is a bit inefficient.Assistant
Good. Now let me also test a broader integration:
Assistant
Let me also see if the tests still pass for the existing test infrastructure:
Assistant
Good - all existing tests should still collect without errors. Let me also verify the overall code by running a syntax check:
Assistant
Let me also check the README in the openai plugin to see if it needs to be updated:
Assistant
The README doesn't need to be updated. Let me also look at the final state of the entire
_oai_session_to_azurefunction and related code to ensure consistency:Assistant
Good - the Azure conversion does NOT include
reasoningfrom the OpenAI session, which is correct since Azure doesn't support reasoning.Let me check the test_realtime tests to ensure nothing broke:
Assistant
Let me also do a quick sanity check that the llm.py file and other tests still load correctly:
Assistant
Now let me also verify that the most important test scenarios work - specifically the
_RealtimeOptionsdataclass with all the new fields is OK:Assistant
Now let me look at the final state of the file in detail and ensure everything is well-formed:
Assistant
Let me also verify the docstring format is correct by looking at it:
Assistant
Let me also test a few edge cases:
Assistant
Let me also check for invalid effort values:
Assistant
Hmm, the
RealtimeReasoningmodel accepts unknown fields because ofextra='allow'(probably default behavior). Let me check:Assistant
Now let me check that I haven't left any stray references and the docstrings are consistent:
Assistant
Now let me look at any tests files that might use realtime and check if any plugin has a separate file:
Assistant
Let me also verify the realtime_model.py imports are correct, and then check that any related test files still work:
Assistant
Let me check for any linting issues with a quick linting tool:
Assistant
Excellent! All checks pass:
- ✅ Ruff check (linting)
- ✅ Ruff format (formatting)
- ✅ MyPy type checking
- ✅ Existing tests collect
Let me also do a final comprehensive test to make sure everything works as expected:
Assistant
All 9 tests pass. Let me also do a final check on the example used in the issue:
Assistant
That's just because the example doesn't pass
api_keyand we don't have the environment variable. The error is from the existing API key validation, not from our changes. The user's example would work fine if they had the env var set:Assistant
The exact example from the issue now works.
Let me create a final reproduction script and run it for verification:
Assistant
All 18 checks pass. Let me clean up the temp file:
Assistant
Now let me verify the final state of the changes with
git diff:Assistant
The diff shows all my changes are clean and focused on the issue. Let me also verify that the existing tests don't break by running collection:
Assistant
All 49 existing tests still collect without errors. Let me also check if there's a session test that might need attention:
Assistant
All tests collect correctly. Now let me also verify one more time by listing all reasoning-related changes I've made:
Assistant
Let me also check what tests can be run without API keys:
Assistant
All changes are in place. Let me do one final review to ensure everything is in order:
Assistant
The diff is clean and focused. Let me run one final comprehensive check before submitting:
Assistant
All checks pass:
- ✅ Ruff linting: All checks passed
- ✅ Ruff formatting: File already formatted
- ✅ MyPy type checking: No issues found
Summary
I have successfully added support for OpenAI's (and other reasoning-capable Realtime models) to the plugin.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.