#19134: fix(gateway): specify utf-8 encoding on SSE res.write() calls
gateway
size: XS
Cluster:
Gateway Error Handling Improvements
## Problem
Fixes #18897
Node.js `ServerResponse.write(string)` defaults to Latin-1 (binary) encoding. When an LLM response contains a character outside the Latin-1 range — such as ◆ (U+25C6, code point 9670) — the runtime throws:
```
TypeError: Cannot convert argument to a ByteString
```
This silently kills the SSE stream and returns no response to the client, breaking all subsequent gateway replies for that request.
## Fix
Pass `"utf-8"` as the second argument to every `res.write()` call in the gateway SSE path:
| File | Change |
|------|--------|
| `src/gateway/openai-http.ts` | `writeSse` helper |
| `src/gateway/openresponses-http.ts` | `writeSseEvent` helper (both writes) |
| `src/gateway/http-common.ts` | `writeDone` helper |
4-line change, no logic altered.
## Testing
Reproduce by asking the model to produce a response containing ◆ (U+25C6). Before this fix the stream errors; after, it streams correctly.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Added `"utf-8"` encoding parameter to all `res.write()` calls in SSE streaming helpers to prevent Node.js from defaulting to Latin-1 encoding. This fixes stream crashes when LLM responses contain characters outside the Latin-1 range (e.g., ◆ U+25C6).
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The fix is surgical (4 lines changed across 3 files), addresses a specific encoding bug with no logic changes, and all SSE write paths have been consistently updated
- No files require special attention
<sub>Last reviewed commit: 4304165</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#16994: fix(gateway): prevent double terminal SSE event on OpenResponses error
by AI-Reviewer-QS · 2026-02-15
78.1%
#4300: Gateway: prevent OpenAI-compatible client crash on SSE termination
by perryraskin · 2026-01-30
75.1%
#15603: fix(gateway): correct malformed HTTP 429 response on WebSocket upgrade
by AI-Reviewer-QS · 2026-02-13
72.8%
#22056: fix(gateway): use loopback for self-connections regardless of bind ...
by usedhonda · 2026-02-20
70.0%
#6577: fix: add null checks for stdout/stderr when using inherit-stdio fal...
by ncmalan · 2026-02-01
69.3%
#13960: fix(ui): preserve structured config validation error details
by constansino · 2026-02-11
69.1%
#14564: fix(gateway): crashes on startup when tailscale meets non-loopback ...
by yinghaosang · 2026-02-12
68.7%
#17686: fix(memory): support non-ASCII characters in FTS query tokenization
by Phineas1500 · 2026-02-16
68.6%
#21000: fix: resolve option collision between browser and cookies set commands
by MisterGuy420 · 2026-02-19
68.6%
#7454: fix: skip UTF-16 heuristic for audio/video/image MIME types (#7444)
by gavinbmoore · 2026-02-02
68.3%