From c9ea80af28e586c9cc1f643b365cdba82f80c700 Mon Sep 17 00:00:00 2001 From: Afonso Menegola Date: Thu, 18 Sep 2025 09:47:28 -0700 Subject: [PATCH] fix: Prevent escaping of Latin characters in LLM response MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Merge https://github.com/google/adk-python/pull/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=https://github.com/google/adk-python/pull/2937 from amenegola:fix/issue-2936-escape-chars 6cac00f97aa4cd8d8ccaa97ec5fffc74f57995dc PiperOrigin-RevId: 808622892 --- .../llm_flows/_output_schema_processor.py | 2 +- .../llm_flows/test_output_schema_processor.py | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/google/adk/flows/llm_flows/_output_schema_processor.py b/src/google/adk/flows/llm_flows/_output_schema_processor.py index da793f8f..d8a3f47f 100644 --- a/src/google/adk/flows/llm_flows/_output_schema_processor.py +++ b/src/google/adk/flows/llm_flows/_output_schema_processor.py @@ -107,7 +107,7 @@ def get_structured_model_response(function_response_event: Event) -> str | None: for func_response in function_response_event.get_function_responses(): if func_response.name == 'set_model_response': # Convert dict to JSON string - return json.dumps(func_response.response) + return json.dumps(func_response.response, ensure_ascii=False) return None diff --git a/tests/unittests/flows/llm_flows/test_output_schema_processor.py b/tests/unittests/flows/llm_flows/test_output_schema_processor.py index 70eedcc6..a9dea8c9 100644 --- a/tests/unittests/flows/llm_flows/test_output_schema_processor.py +++ b/tests/unittests/flows/llm_flows/test_output_schema_processor.py @@ -254,6 +254,39 @@ async def test_output_schema_helper_functions(): assert extracted_json is None +@pytest.mark.asyncio +async def test_get_structured_model_response_with_non_ascii(): + """Test get_structured_model_response with non-ASCII characters.""" + from google.adk.events.event import Event + from google.adk.flows.llm_flows._output_schema_processor import get_structured_model_response + from google.genai import types + + # Test with a dictionary containing non-ASCII characters + test_dict = {'city': 'São Paulo'} + expected_json = '{"city": "São Paulo"}' + + # Create a function response event + function_response_event = Event( + author='test_agent', + content=types.Content( + role='user', + parts=[ + types.Part( + function_response=types.FunctionResponse( + name='set_model_response', response=test_dict + ) + ) + ], + ), + ) + + # Get the structured response + extracted_json = get_structured_model_response(function_response_event) + + # Assert that the output is the expected JSON string without escaped characters + assert extracted_json == expected_json + + @pytest.mark.asyncio async def test_end_to_end_integration(): """Test the complete output schema with tools integration."""