fix(agent): sanitize surrogate characters from Ollama model output - #5176
Closed
tmchow wants to merge 1 commit into
Closed
fix(agent): sanitize surrogate characters from Ollama model output#5176tmchow wants to merge 1 commit into
tmchow wants to merge 1 commit into
Conversation
Models served through Ollama (Kimi K2.5, GLM-5, Qwen) can return invalid surrogate code points (U+D800-U+DFFF) that crash json.dumps() during UTF-8 serialization. Three gaps in the existing surrogate handling: 1. No proactive sanitization before the API call. Surrogates from prior model output or tool results survive in api_messages and crash on the next turn. Fixed by calling _sanitize_messages_surrogates() on api_messages after _sanitize_api_messages(). 2. API responses stored without sanitization. Surrogates from model output persist in session history via _build_assistant_message(). Fixed by sanitizing content and reasoning_text before building the message dict. 3. Retry path sanitized the wrong variable. The UnicodeEncodeError handler at line ~7366 called _sanitize_messages_surrogates(messages) but the API call uses api_messages (a separate list). The retry would fail again with the same error. Fixed by sanitizing both api_messages and messages. Fixes NousResearch#5059
1 task
Contributor
Author
This was referenced Aug 3, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
Fixes three gaps in the existing surrogate character sanitization that cause
UnicodeEncodeErrorcrashes when using Ollama-hosted models (Kimi K2.5, GLM-5, Qwen). These models can return invalid surrogate code points (U+D800-U+DFFF) that crashjson.dumps()during UTF-8 serialization.The existing
_sanitize_surrogates()and_sanitize_messages_surrogates()functions work correctly, but they're not called in the right places:No proactive sanitization before the API call (line 6848). Surrogates from prior model output or tool results survive in
api_messagesand crash on the next turn. Fixed by calling_sanitize_messages_surrogates(api_messages)after_sanitize_api_messages().API responses stored without sanitization (line 5242). Surrogates from model output persist in session history via
_build_assistant_message(). Fixed by sanitizingcontentandreasoning_textbefore building the message dict.Retry path sanitized the wrong variable (line 7368). The
UnicodeEncodeErrorhandler called_sanitize_messages_surrogates(messages)but the API call usesapi_messages(a separate list). The retry would fail again with the same error. Fixed by sanitizing bothapi_messagesandmessages.Related Issue
Fixes #5059
Type of Change
Changes Made
run_agent.py:6848: Added_sanitize_messages_surrogates(api_messages)call after_sanitize_api_messages()for proactive surrogate cleaning before the API callrun_agent.py:5242: Sanitizeassistant_message.contentandreasoning_textwith_sanitize_surrogates()before storing in message historyrun_agent.py:7368: Changed_sanitize_messages_surrogates(messages)to also sanitizeapi_messagesso the retry actually cleans the list sent to the APIHow to Test
UnicodeEncodeErrorcrash occurspytest tests/test_surrogate_sanitization.pyto verify existing tests still passChecklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/AThis contribution was developed with AI assistance (Claude Code).