Skip to content

[CB-0] [sdk-superfan] Let fetch set content-length - #241

Draft
spicysouvlaki wants to merge 1 commit into
mainfrom
wschmitt/drop-content-length
Draft

spicysouvlaki wants to merge 1 commit into
mainfrom
wschmitt/drop-content-length

Conversation

@spicysouvlaki

@spicysouvlaki spicysouvlaki commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

What happened

#197: with a userland undici dispatcher installed via setGlobalDispatcher(), every sessions.create fails with InvalidArgumentError: invalid content-length header (UND_ERR_INVALID_ARG).

Why

buildHeaders() always sets content-length. Since v2.14.1 the Node runtime sends requests through native fetch (runtime-node.ts:51). undici fetch before 7.26 / 8.3 (bundled in Node 18, 20 and 22) appends its own value, so the header becomes "52, 52". A dispatcher from undici 7.26+ or 8.3+ rejects that value. Node 24.19 bundles undici 7.29, which no longer appends, so it does not reproduce there.

Fix

Stop setting content-length. Native fetch and node-fetch both compute it from the body. Multipart uploads keep their explicit length.

Tests

  • Replaced the two buildRequest content-length assertions with one that checks the header is unset (fails on main: Received: "20"). Added a local-server test that checks exactly one correct content-length arrives, under native fetch and under node-fetch.
  • On the dev API (Node 24.19, undici 8.8.0 dispatcher): with undici 6.21.1 fetch (the version Node 22.14 bundles), main throws the reported error and this branch creates and releases a session. Native fetch works on both.
  • ./scripts/lint, ./scripts/test (203 passed), ./scripts/build all pass.
  • Not run: a real Node 22 binary (none installed). I used undici 6.21.1 fetch in its place.

Risk and rollout

src/core.ts is generated, so this is a sealed edit that may conflict on the next Stainless regen. It rolls out with the next SDK release. It removes a header that both supported fetch implementations already set.

Fixes #197

🤖 Generated with Claude Code

buildHeaders() set content-length from calculateContentLength() on every
request with a body. Since #191 (v2.14.1) the auto Node runtime routes
requests through native fetch, so that header now meets undici's fetch.

undici fetch < 7.26 / < 8.3 (Node 18/20/22 bundled builds, e.g. Node 22.14
bundles 6.21.1) appends its own computed Content-Length to the caller's in
httpNetworkOrCacheFetch, giving "52, 52". The bundled dispatcher parses that
leniently with parseInt, so nobody noticed. A userland undici >= 7.26 / 8.3
dispatcher installed with setGlobalDispatcher() validates the value with
isValidContentLengthHeaderValue() in core/request.js and throws
InvalidArgumentError: invalid content-length header. undici 7.26 / 8.3 also
stopped appending when the header already exists, so Node 24.19 (bundled
undici 7.29) does not reproduce.

Measured on Node 24.19 against api.dev.browserbase.com, sessions.create:
- main, native fetch + undici 8.8.0 dispatcher: request accepted (429 burst
  limit, headers fine)
- main, undici 6.21.1 fetch (Node 22.14 stand-in) + undici 8.8.0 dispatcher:
  APIConnectionError, cause InvalidArgumentError invalid content-length header
  UND_ERR_INVALID_ARG
- this commit, same two combos: OK, session released
Local server sweep: fetch 6.21.1 or 7.24.0 with dispatcher 7.26.0 / 8.8.0 /
8.12.4 all throw on main.

Both runtimes compute the header themselves: native fetch from the body,
node-fetch v2 via getTotalBytes() for strings and ArrayBufferViews. Multipart
streams keep their explicit Content-Length from getMultipartRequestOptions().

src/core.ts is generated; this edit is sealed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

No deployments
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.

content-length header collision → InvalidArgumentError: invalid content-length header when a custom undici global dispatcher is installed

1 participant