-
Notifications
You must be signed in to change notification settings - Fork 3.1k
fix(aws): unwrap doubly-encoded JSON tool arguments from Nova Sonic #5411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
theomonnom
merged 3 commits into
livekit:main
from
rililinx:fix/nova-sonic-tool-args-json-parse
Apr 11, 2026
+177
−7
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
166 changes: 166 additions & 0 deletions
166
livekit-plugins/livekit-plugins-aws/tests/test_nova_sonic_tool_args.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| """ | ||
| Regression tests for Nova Sonic tool-call argument parsing. | ||
|
|
||
| Nova Sonic may deliver toolUse.content as a doubly-encoded JSON string — a | ||
| JSON string whose value is itself a JSON object string. When this reaches | ||
| prepare_function_arguments, pydantic_core.from_json() returns a Python str | ||
| instead of a dict, causing: | ||
|
|
||
| TypeError: string indices must be integers, not 'str' (utils.py:404) | ||
|
|
||
| The fix peels off one encoding layer *only* when the inner string is itself a | ||
| valid JSON object. Legitimate string-valued schemas (e.g. content="hello") | ||
| must be left untouched so that raw tool schemas with primitive top-level types | ||
| continue to work correctly. | ||
| """ | ||
|
|
||
| import json | ||
| import sys | ||
| from unittest.mock import MagicMock | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Stub out the optional AWS Smithy/Bedrock SDK not installed in the base venv. | ||
| # --------------------------------------------------------------------------- | ||
| _AWS_STUBS = [ | ||
| "aws_sdk_bedrock_runtime", | ||
| "aws_sdk_bedrock_runtime.client", | ||
| "aws_sdk_bedrock_runtime.models", | ||
| "aws_sdk_bedrock_runtime.config", | ||
| "smithy_aws_core", | ||
| "smithy_aws_core.identity", | ||
| "smithy_aws_event_stream", | ||
| "smithy_aws_event_stream.exceptions", | ||
| "smithy_core", | ||
| "smithy_core.aio", | ||
| "smithy_core.aio.interfaces", | ||
| "smithy_core.aio.interfaces.identity", | ||
| ] | ||
| for _mod in _AWS_STUBS: | ||
| if _mod not in sys.modules: | ||
| sys.modules[_mod] = MagicMock() | ||
|
|
||
|
|
||
| def _make_tool_event(content) -> dict: | ||
| return { | ||
| "event": { | ||
| "toolUse": { | ||
| "toolUseId": "test-id-123", | ||
| "toolName": "check_availability", | ||
| "content": content, | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| def _make_fake_session(captured: list) -> MagicMock: | ||
| ch = MagicMock() | ||
| ch.send_nowait = lambda call: captured.append(call) | ||
|
|
||
| generation = MagicMock() | ||
| generation.function_ch = ch | ||
|
|
||
| session = MagicMock() | ||
| session._current_generation = generation | ||
| session._pending_tools = set() | ||
| session._close_current_generation = MagicMock() | ||
| return session | ||
|
|
||
|
|
||
| class TestHandleToolOutputContentEvent: | ||
| """Unit tests for _handle_tool_output_content_event.""" | ||
|
|
||
| async def test_doubly_encoded_string_is_unwrapped(self): | ||
| """Bug case: content is a JSON string wrapping another JSON string. | ||
|
|
||
| Nova Sonic sends: '"{\\"input\\":{\\"date\\":\\"2026-04-10\\"}}"' | ||
| After fix: '{"input":{"date":"2026-04-10"}}' (one layer removed) | ||
| """ | ||
| from livekit.plugins.aws.experimental.realtime.realtime_model import ( | ||
| RealtimeSession, | ||
| ) | ||
|
|
||
| captured = [] | ||
| session = _make_fake_session(captured) | ||
|
|
||
| inner_json = json.dumps({"input": {"date": "2026-04-10"}}) | ||
| doubly_encoded = json.dumps(inner_json) # wrap in another JSON string | ||
| event = _make_tool_event(doubly_encoded) | ||
|
|
||
| await RealtimeSession._handle_tool_output_content_event(session, event) | ||
|
|
||
| assert len(captured) == 1 | ||
| # arguments must be the inner JSON string (one layer removed), not the | ||
| # doubly-encoded original | ||
| assert captured[0].arguments == inner_json | ||
|
|
||
| async def test_single_encoded_string_passed_through(self): | ||
| """Normal case: content is already a proper JSON object string.""" | ||
| from livekit.plugins.aws.experimental.realtime.realtime_model import ( | ||
| RealtimeSession, | ||
| ) | ||
|
|
||
| captured = [] | ||
| session = _make_fake_session(captured) | ||
|
|
||
| json_str = json.dumps({"input": {"date": "2026-04-10"}}) | ||
| event = _make_tool_event(json_str) | ||
|
|
||
| await RealtimeSession._handle_tool_output_content_event(session, event) | ||
|
|
||
| assert len(captured) == 1 | ||
| assert captured[0].arguments == json_str | ||
|
|
||
| async def test_invalid_json_string_does_not_crash(self): | ||
| """Invalid JSON string → plugin leaves it as-is rather than raising.""" | ||
| from livekit.plugins.aws.experimental.realtime.realtime_model import ( | ||
| RealtimeSession, | ||
| ) | ||
|
|
||
| captured = [] | ||
| session = _make_fake_session(captured) | ||
|
|
||
| event = _make_tool_event("not-valid-json") | ||
|
|
||
| await RealtimeSession._handle_tool_output_content_event(session, event) | ||
|
|
||
| assert len(captured) == 1 | ||
| assert captured[0].arguments == "not-valid-json" | ||
|
|
||
| async def test_string_primitive_schema_not_unwrapped(self): | ||
| """Regression: content is a JSON string literal (valid primitive schema). | ||
|
|
||
| Bedrock raw tool schemas may legitimately pass a string value such as | ||
| '"hello"'. This must NOT be unwrapped to 'hello' (which would be invalid | ||
| JSON and cause from_json() to fail downstream). | ||
| """ | ||
| from livekit.plugins.aws.experimental.realtime.realtime_model import ( | ||
| RealtimeSession, | ||
| ) | ||
|
|
||
| captured = [] | ||
| session = _make_fake_session(captured) | ||
|
|
||
| string_arg = json.dumps("hello") # produces '"hello"' | ||
| event = _make_tool_event(string_arg) | ||
|
|
||
| await RealtimeSession._handle_tool_output_content_event(session, event) | ||
|
|
||
| assert len(captured) == 1 | ||
| # Must be the original '"hello"', not the bare string 'hello' | ||
| assert captured[0].arguments == string_arg | ||
|
|
||
| async def test_tool_name_and_id_forwarded_correctly(self): | ||
| """call_id and name are passed through regardless of args format.""" | ||
| from livekit.plugins.aws.experimental.realtime.realtime_model import ( | ||
| RealtimeSession, | ||
| ) | ||
|
|
||
| captured = [] | ||
| session = _make_fake_session(captured) | ||
|
|
||
| event = _make_tool_event(json.dumps({"date": "2026-04-10"})) | ||
|
|
||
| await RealtimeSession._handle_tool_output_content_event(session, event) | ||
|
|
||
| assert captured[0].call_id == "test-id-123" | ||
| assert captured[0].name == "check_availability" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.