Skip to content

Commit c9ea80a

Browse files
amenegolacopybara-github
authored andcommitted
fix: Prevent escaping of Latin characters in LLM response
Merge #2937 **Closes #2936** This Pull Request addresses the issue where `LlmAgent` outputs, when configured with `output_schema` and `tools`, were presenting escaped Latin characters (e.g., `\xf3` for `ó`) in the final response. This behavior occurred because `json.dumps` was being called with `ensure_ascii=True` (its default), which is not ideal for human-readable output, especially when dealing with non-ASCII characters common in many languages like Portuguese. **Changes Proposed:** * Modified the `_OutputSchemaRequestProcessor` in `src/google/adk/flows/llm_flows/_output_schema_processor.py` to explicitly set `ensure_ascii=False` when calling `json.dumps` for the `set_model_response` tool's output. **Impact:** This change ensures that all non-ASCII characters in the structured model response are preserved in their natural form, improving the readability and user experience of agent outputs, particularly for users interacting in languages with accented characters or other special symbols. **Testing:** The fix was verified locally by running an `LlmAgent` with an `output_schema` and confirming that responses containing Latin characters (e.g., "ação", "caminhão", "ícone") are now correctly displayed without escaping. COPYBARA_INTEGRATE_REVIEW=#2937 from amenegola:fix/issue-2936-escape-chars 6cac00f PiperOrigin-RevId: 808622892
1 parent 86ee6e3 commit c9ea80a

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/google/adk/flows/llm_flows/_output_schema_processor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def get_structured_model_response(function_response_event: Event) -> str | None:
107107
for func_response in function_response_event.get_function_responses():
108108
if func_response.name == 'set_model_response':
109109
# Convert dict to JSON string
110-
return json.dumps(func_response.response)
110+
return json.dumps(func_response.response, ensure_ascii=False)
111111

112112
return None
113113

tests/unittests/flows/llm_flows/test_output_schema_processor.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,39 @@ async def test_output_schema_helper_functions():
254254
assert extracted_json is None
255255

256256

257+
@pytest.mark.asyncio
258+
async def test_get_structured_model_response_with_non_ascii():
259+
"""Test get_structured_model_response with non-ASCII characters."""
260+
from google.adk.events.event import Event
261+
from google.adk.flows.llm_flows._output_schema_processor import get_structured_model_response
262+
from google.genai import types
263+
264+
# Test with a dictionary containing non-ASCII characters
265+
test_dict = {'city': 'São Paulo'}
266+
expected_json = '{"city": "São Paulo"}'
267+
268+
# Create a function response event
269+
function_response_event = Event(
270+
author='test_agent',
271+
content=types.Content(
272+
role='user',
273+
parts=[
274+
types.Part(
275+
function_response=types.FunctionResponse(
276+
name='set_model_response', response=test_dict
277+
)
278+
)
279+
],
280+
),
281+
)
282+
283+
# Get the structured response
284+
extracted_json = get_structured_model_response(function_response_event)
285+
286+
# Assert that the output is the expected JSON string without escaped characters
287+
assert extracted_json == expected_json
288+
289+
257290
@pytest.mark.asyncio
258291
async def test_end_to_end_integration():
259292
"""Test the complete output schema with tools integration."""

0 commit comments

Comments
 (0)