fix: let fetch calculate request content length#195
Open
shrey150 wants to merge 2 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
The SDK currently calculates
Content-Lengthbefore handing a request to the configured Fetch implementation. That value becomes stale if Fetch middleware transforms the body afterward, and Node's native Fetch then rejects the request withUND_ERR_REQ_CONTENT_LENGTH_MISMATCH.The ordinary session-create JSON path is byte-correct on its own. The compatibility problem is ownership: Fetch already calculates request framing from the final body, and current Stainless-generated TypeScript clients leave that work to the transport.
What changed
content-lengthin the SDK request buildernode-fetchfallbackRoot cause
The length calculation itself predates the reported regression. SDK 2.14.1 changed modern Node runtimes from
node-fetchto nativeglobalThis.fetch; native Fetch/Undici strictly validates an explicit length against the final bytes. A body-transforming wrapper can therefore expose a stale header that the earlier transport path did not surface. Delegating the header to Fetch makes it calculate the value after those transformations.E2E Test Matrix
node@22 <harness> local minifyContent-Length: 159; server received 159 bytes; PASS.node@24 <harness> local minifyContent-Length: 159; server received 159 bytes; PASS.node@22 <harness> local node-fetchandnode@24 <harness> local node-fetchnode-fetchgeneratedContent-Length: 217; server received 217 bytes; PASS.node@22 jest tests/content-length.test.tsandnode@24 jest tests/content-length.test.tsnode-fetchfallback, and body-transforming middleware.node@22 <local Stagehand 3.7 flow>andnode@24 <local Stagehand 3.7 flow>stagehand.init()created a session andclose()released it on both runtimes; PASS.node@22 <local SDK flow>andnode@24 <local SDK flow>node@20 jest --runInBandwith the generated mock APInpm run buildon Node 20npm run lint