Skip to content

TypeError: terminated — TLS socket closes mid-stream during SSE response #412

@ldriss

Description

@ldriss

Hi Team

Environment:

  • Runtime: Node.js (Bun) with native fetch (undici)
  • SDK: AI SDK v6 + @openrouter/ai-sdk-provider
  • Endpoint: POST /api/v1/chat/completions (streaming)

Error:
TypeError: terminated
at Fetch.onAborted (node:internal/deps/undici/undici:12707:53)
at Fetch.emit (node:events:508:28)
at Fetch.terminate (node:internal/deps/undici/undici:11866:14)
at Object.onError (node:internal/deps/undici/undici:12831:38)
at Request.onError (node:internal/deps/undici/undici:3019:31)
at Object.errorRequest (node:internal/deps/undici/undici:1897:17)
at TLSSocket.onHttpSocketClose (node:internal/deps/undici/undici:7637:14)
at TLSSocket.emit (node:events:520:35)
at node:net:346:12
at TCP.done (node:_tls_wrap:666:7)

Behavior:

  • The streaming request starts successfully but the server closes the TLS connection before the response completes
  • The error has no HTTP statusCode, no errorCode, no responseBody — just a raw TypeError with message "terminated"
  • This makes it impossible to distinguish from client-side network issues or to apply retry logic based on status codes

Thanks

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions