Fix runTools readable stream round trip tool results#1986
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6e82c4d23b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
pushed a follow-up in verified with |
Summary
When a streaming
runTools()run is serialized withtoReadableStream()and reconstructed withfromReadableStream(), tool result messages were lost. The proxied runner kept assistant messages rebuilt from chunks, but missed{ role: 'tool' }messages and the relatedfunctionToolCallResultevent.Root cause
The readable stream protocol only wrote raw
ChatCompletionChunklines. Local tool result messages are added by the runner after tool execution, so they were never present in the serialized stream.Fix
ChatCompletionStreamingRunner.toReadableStream()now keeps normal chunk lines unchanged and adds tool result messages as a small envelope line shaped{ type: 'message', message }.fromReadableStream()consumes that envelope as a real message and uses it as a completion boundary. The stream end guard now accepts a stream that ends after a tool message while preservingrequest ended without sending any chunksfor truly empty streams.Compatibility
Old raw chunk streams still reconstruct the same way.
ChatCompletionChunkhas no top leveltypefield, so an old chunk is not mistaken for a message envelope. Assistant messages are still rebuilt from chunks, so this does not duplicate assistant messages.Scope
The PR template warns that this SDK is generated. This change is confined to
src/lib, which is hand written and not generator owned. Tests are confined totests/lib.Tests
./scripts/test tests/lib tests/api-resources682 passed. No failures.
Fixes #1060