instructor icon indicating copy to clipboard operation
instructor copied to clipboard

Partial streaming drops JSON-valid DEL and C1 characters

Open resonance-null opened this issue 1 month ago • 0 comments

Bug

Partial JSON streaming silently removes DEL and C1 characters from legal string data. Literal and escaped JSON spellings of the same string produce different extracted values.

Offline reproduction

import json
from pydantic import BaseModel
from instructor.dsl.partial import Partial

class Text(BaseModel):
    text: str

value = "A" + chr(0x85) + "B"
payload = json.dumps({"text": value}, ensure_ascii=False)
result = list(Partial[Text].model_from_chunks([payload]))[-1]
print(repr(result.text))

At source main 6cd771c5254a69f6a6cf9cdf357fb2f9523f0b85, the parsed value loses U+0085. The expected value is the exact original string, matching Text.model_validate_json(payload). Changing only ensure_ascii=True preserves it. The same behavior occurs for U+007F through U+009F, across character-sized chunks and in the async method.

The public client.chat.completions.create_partial path also reproduces the loss in sync and async OpenAI JSON mode using real SDK clients with synthetic httpx.MockTransport SSE responses. No provider/model request is needed.

remove_control_chars strips \x7F-\x9F, although those characters are valid unescaped inside JSON strings under RFC 8259 section 7. Restricting the existing sanitation to C0 leaves malformed-C0 recovery unchanged and preserves valid string data.

Observed on Linux/Python 3.12.8 with Pydantic v2 and current source. Model/messages are not applicable to the minimal local reproduction; SDK checks use synthetic responses only. AI assistance: OpenAI Codex investigated, implemented and locally tested the proposed change; separate root AI review reran the public clients and related tests. No human code review is claimed.

resonance-null avatar Nov 02 '26 00:11 resonance-null