Skip to content

The agent that grows with you

fix: surface ascii-codec error when UTF-8 runtime repairs nothing

kshitijk4poor05287a536999
diff --git a/agent/turn_recovery.py b/agent/turn_recovery.py
index 3072b9b9c38..19b6dc33146 100644
--- a/agent/turn_recovery.py
+++ b/agent/turn_recovery.py
@@ -148,7 +148,9 @@ def _recover_unicode_encode_error(
# Error text is provider-controlled and can mention ``ascii`` even when the
# process sends UTF-8. In that normal case, do not rewrite conversation,
# tools, prompts, or prefill; only repair values that can poison an ASCII
- # transport header and retry the unchanged request copy.
+ # transport header. If nothing was repaired, an identical retry cannot
+ # succeed — return False so the error surfaces through the normal path
+ # instead of burning both sanitization passes on unchanged requests.
if not _runtime_uses_ascii_encoding():
agent._force_ascii_payload = False
_client_kwargs = getattr(agent, "_client_kwargs", None)
@@ -165,12 +167,12 @@ def _recover_unicode_encode_error(
if getattr(agent, "client", None) is not None and hasattr(agent.client, "api_key"):
agent.client.api_key = _clean_key
_credential_sanitized = True
+ if not (_headers_sanitized or _credential_sanitized):
+ return False, active_system_prompt
agent._unicode_sanitization_passes += 1
_vlines(
agent,
- "⚠️ Repaired non-ASCII request credentials/headers without changing conversation content. Retrying..."
- if (_headers_sanitized or _credential_sanitized) else
- "⚠️ ASCII codec error under UTF-8 runtime — retrying unchanged request content...",
+ "⚠️ Repaired non-ASCII request credentials/headers without changing conversation content. Retrying...",
)
return True, active_system_prompt
diff --git a/tests/agent/test_unicode_ascii_codec.py b/tests/agent/test_unicode_ascii_codec.py
index ffd880dfba5..12092821975 100644
--- a/tests/agent/test_unicode_ascii_codec.py
+++ b/tests/agent/test_unicode_ascii_codec.py
@@ -388,7 +388,10 @@ class TestSanitizeMessagesPersistMarker:
canonical, api_messages, {}, "system ☕",
)
- assert recovered is True
+ # Nothing was repaired, so an identical retry cannot help: the error must
+ # surface through the normal path rather than consume a sanitization pass.
+ assert recovered is False
+ assert agent._unicode_sanitization_passes == 0
assert canonical[0]["content"] == "olá ☕"
assert api_messages[0]["content"] == "olá ☕"
assert agent._cached_system_prompt == "system ☕"