Skip to content

fix(realtime): preserve Unicode characters in realtime calls session serialization - #3710

Open
piyushrajyadav wants to merge 1 commit into
openai:mainfrom
piyushrajyadav:fix/2428-unicode-json-realtime
Open

fix(realtime): preserve Unicode characters in realtime calls session serialization#3710
piyushrajyadav wants to merge 1 commit into
openai:mainfrom
piyushrajyadav:fix/2428-unicode-json-realtime

Conversation

@piyushrajyadav

Copy link
Copy Markdown

Summary

This PR ensures non-ASCII Unicode characters (such as Cyrillic, Chinese, Japanese, Arabic, etc.) in the session payload for realtime.calls.create (sync & async) are not converted to \uXXXX escape sequences during serialization.

Problem

When passing instructions, names, or other parameters containing non-ASCII characters to client.realtime.calls.create(..., session=...), Python's json.dumps() defaults to ensure_ascii=True, converting characters like Привет into \u041f\u0440\u0438\u0432\u0435\u0442. This significantly bloats the payload size and leads to excess token consumption upstream.

Solution

  • Added ensure_ascii=False to json.dumps() in both _Calls.create and _AsyncCalls.create in src/openai/lib/_realtime.py.
  • Added unit tests in tests/test_realtime_calls_unicode.py to verify that Unicode characters remain unescaped UTF-8 in both sync and async request payloads.

Fixes #2428

How it was tested

Ran the test suite locally:

python -m pytest -s -vv -n0 tests/test_realtime_calls_unicode.py

Output:

============================= test session starts =============================
platform win32 -- Python 3.14.7, pytest-9.1.1, pluggy-1.6.0
rootdir: E:\open source\open ai\openai-python
configfile: pyproject.toml
plugins: anyio-4.14.2, inline-snapshot-0.35.4, langsmith-0.10.17, asyncio-1.4.0, xdist-3.8.0
asyncio: mode=Mode.AUTO, debug=False, asyncio_default_fixture_loop_scope=session, asyncio_default_test_loop_scope=function
collecting ... collected 2 items

tests/test_realtime_calls_unicode.py::TestRealtimeCallsUnicode::test_realtime_create_unicode_session_not_escaped PASSED
tests/test_realtime_calls_unicode.py::TestRealtimeCallsUnicode::test_async_realtime_create_unicode_session_not_escaped PASSED

============================== 2 passed in 1.91s ==============================

Linting:

python -m ruff check src/openai/lib/_realtime.py tests/test_realtime_calls_unicode.py
All checks passed!

@piyushrajyadav
piyushrajyadav requested a review from a team as a code owner August 21, 2026 12:56
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.

Excessive token usage with Cyrillic text in Pydantic schema descriptions due to JSON Unicode escaping

1 participant