Skip to content

Why do multilingual characters become replacement symbols when my client reads an API response in chunks?

Keep decoder state across network chunks, or let a standard bounded whole-response decoder handle the complete JSON. A multibyte UTF-8 character can be divided between arbitrary HTTP byte chunks. Decoding each chunk with a fresh decoder can introduce replacement symbols even when the original response is valid. This does not mean AISwapLab supports streaming chat tokens: its current text contract still requires stream=false.

SHIP A RELIABLE INTEGRATIONAISwapLab team · Editorial methodPublished 3 min read
Open API documentationExplore 67 Image SkillsPrivate keys · Explicit credit approval
Article workflow: Identify where the characters change; Use a consistent bounded decoder; Split the fixture at every byte position.
Workflow diagram: Identify where the characters change → Use a consistent bounded decoder → Split the fixture at every byte position. Read the article for evidence and limits.

Transport chunks are not complete characters or JSON events

RFC 8259 requires interoperable network JSON to use UTF-8. That representation may arrive in several byte chunks without becoming several JSON documents. An input byte ceiling and a response decoder solve different problems. Before attributing altered text to the model, compare the raw synthetic response with your custom reader's decoded result. Normal response.json() is not evidence that you need to build a manual chunk parser; custom processing creates additional responsibilities.

What to do next

  1. 01

    Identify where the characters change

    Use a nonprivate local fixture with known multilingual text inside one valid JSON object. Observe the original bytes, decoding stage and final displayed string separately. Check whether the client creates a new TextDecoder for each chunk, concatenates independently decoded strings or attempts JSON.parse on each transport piece. Do not switch to an event-stream parser simply because the HTTP body is delivered incrementally.

  2. 02

    Use a consistent bounded decoder

    Prefer the platform's normal complete-response JSON path when it meets the application's size policy. When incremental byte processing is necessary, use one stateful decoder with stream continuation for intermediate data and a final flush, or an appropriate TextDecoderStream. Parse the completed representation under the expected response schema. Apply your explicit invalid-encoding and resource policies; choosing a streaming decoder does not itself bound accumulated text or make a truncated JSON document acceptable.

  3. 03

    Split the fixture at every byte position

    Create local chunks that divide a known multibyte character after each possible byte, including an emoji and a non-Latin letter. Verify that the final code points and parsed fields match the fixture regardless of chunk boundaries. Add a truncated final sequence and an incomplete JSON object as separate failures. Preserve the known HTTP/task context when decoding fails instead of silently correcting text or generating another answer. Never use a private customer prompt as the test string.

A concrete example

Illustrative fixture: a JSON message contains the short text cat, followed by a Chinese character and an emoji. The test splits bytes inside both multibyte characters. A fresh-decoder-per-chunk implementation changes the string; the proposed stateful path must retain the original final text. No model completion was requested to produce this scenario, and the example is not evidence of a server encoding fault.

Before you call it ready

  • HTTP byte chunking is distinguished from unsupported token-event streaming.
  • Decoder state survives intermediate chunks and is finalized once.
  • Complete text, truncated encoding and incomplete JSON have distinct fixture expectations.

When to stop or choose another method

Stop rewriting replacement symbols as guessed intended characters. Locate the byte-to-text error, preserve the original task identity and fix the custom decoder before trusting the displayed answer or requesting another paid completion.

Sources

One task. A recoverable workflow.

Check the contract, approve the quote and retain the job.

Open API documentation