Skip to content

fix(OpenAI): trim streamed response data before decoding- #9 - #806

Merged
iBotPeaches merged 11 commits into
openai-php:mainfrom
e4se:diagnose-stream-json
Sep 23, 2026
Merged

iBotPeaches merged 11 commits into
openai-php:mainfrom
e4se:diagnose-stream-json

Conversation

@e4se

@e4se e4se commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

What:

  • Bug Fix
  • New Feature

Description:

Streaming response lines may contain boundary control characters such as a trailing NUL byte. Passing those bytes directly to json_decode() causes a JSON_ERROR_CTRL_CHAR exception even when the JSON payload itself is valid.

This change trims the same boundary characters handled by PHP trim() before decoding the SSE data: value. The extraction calculates the trimmed boundaries first and creates only one resulting string, avoiding the additional full-size allocation produced by trim(substr(...)). This is important for large streamed events such as context compaction payloads, which can be tens of megabytes.

A regression test covers a valid streamed compaction event followed by a NUL byte and verifies that it is decoded successfully.

@iBotPeaches

Copy link
Copy Markdown
Collaborator

Clever trick to reduce a copy - just trim off front/end prior to decode.

@iBotPeaches iBotPeaches added this to the v0.22.0 milestone Sep 23, 2026
@iBotPeaches
iBotPeaches merged commit 69324fa into openai-php:main Sep 23, 2026
14 checks passed
@e4se
e4se deleted the diagnose-stream-json branch September 23, 2026 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants