hermes - ✅(Solved) Fix surrogate sanitization not applied to NVIDIA NIM provider responses [1 pull requests, 2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
NousResearch/hermes-agent#19819Fetched 2026-05-05 06:04:59
View on GitHub
Comments
2
Participants
2
Timeline
7
Reactions
0
Timeline (top)
labeled ×4commented ×2cross-referenced ×1

Fix Action

Fix / Workaround

Workaround: retry the same message usually works (since random sampling avoids the bad surrogate on retry).

PR fix notes

PR #19843: fix(agent): sanitize streamed surrogates before callbacks

Description (problem / solution / changelog)

What does this PR do?

Fixes streamed assistant deltas that contain lone surrogate code points by running them through the existing _sanitize_surrogates() helper before any stream callbacks receive the text.

This prevents downstream UTF-8 consumers from crashing with UnicodeEncodeError when a provider response includes invalid surrogate characters.

Related Issue

Fixes #19819

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 🔒 Security fix
  • 📝 Documentation update
  • ✅ Tests (adding or improving test coverage)
  • ♻️ Refactor (no behavior change)
  • 🎯 New skill (bundled or hub)

Changes Made

  • run_agent.py: sanitize streamed text deltas after think/context scrubbing and before callbacks are invoked.
  • tests/run_agent/test_run_agent_codex_responses.py: add a regression test for a streamed delta containing \udce7.

How to Test

  1. Run the regression test:
    python -m pytest tests/run_agent/test_run_agent_codex_responses.py::test_stream_delta_replaces_surrogates_before_callbacks -q
  2. Run the focused streaming/Codex response tests:
    python -m pytest tests/run_agent/test_run_agent_codex_responses.py -q
  3. Run the broader run_agent unit tests:
    python -m pytest tests/run_agent/test_run_agent.py -q

Validated on Ubuntu 24.04 with Python 3.11.15.

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: Ubuntu 24.04

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

Screenshots / Logs

python -m pytest tests/run_agent/test_run_agent_codex_responses.py::test_stream_delta_replaces_surrogates_before_callbacks -q
1 passed

python -m pytest tests/run_agent/test_run_agent_codex_responses.py -q
60 passed

python -m pytest tests/run_agent/test_run_agent.py -q
314 passed

Changed files

  • run_agent.py (modified, +1/-0)
  • tests/run_agent/test_run_agent_codex_responses.py (modified, +11/-0)
RAW_BUFFERClick to expand / collapse

I'm running Hermes v0.12.0 with NVIDIA NIM (minimaxai/minimax-m2.7) on Discord. Getting intermittent UnicodeEncodeError ('utf-8' codec can't encode character '\udce7' in position 1: surrogates not allowed).

Issue #5059 added surrogate sanitization for Ollama, but the same fix doesn't seem to apply to NIM provider responses. Same surrogate problems happen when the M2.7 model occasionally emits malformed multi-byte sequences.

Workaround: retry the same message usually works (since random sampling avoids the bad surrogate on retry).

Request: extend _sanitize_messages_surrogates to cover responses from all non-OpenAI/Anthropic providers, not just Ollama.

extent analysis

TL;DR

Extend the _sanitize_messages_surrogates function to cover responses from all non-OpenAI/Anthropic providers to fix the UnicodeEncodeError.

Guidance

  • Identify the source of the malformed multi-byte sequences in the M2.7 model responses to understand why surrogates are being emitted.
  • Extend the _sanitize_messages_surrogates function to handle responses from the NIM provider, as currently, it only applies to Ollama.
  • Verify that the extended function correctly sanitizes surrogates in responses from all non-OpenAI/Anthropic providers.
  • Consider implementing a retry mechanism with a limited number of attempts to handle cases where the surrogate sanitization fails.

Example

No code example is provided due to the lack of specific implementation details in the issue.

Notes

The provided workaround of retrying the message usually works, but a more robust solution is needed to handle the root cause of the issue. The fix should be applied to the _sanitize_messages_surrogates function to ensure it covers all non-OpenAI/Anthropic providers.

Recommendation

Apply workaround: Implement the extended _sanitize_messages_surrogates function to cover responses from all non-OpenAI/Anthropic providers, as this will provide a more robust solution to the intermittent UnicodeEncodeError issue.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

hermes - ✅(Solved) Fix surrogate sanitization not applied to NVIDIA NIM provider responses [1 pull requests, 2 comments, 2 participants]