Skip to content

AI Gateway corrupts binary AWS Event Stream framing (Bedrock streaming) #471

@danielgangl

Description

@danielgangl

Summary

AWS Bedrock streaming (invoke-with-response-stream) is broken through AI Gateway. Non-streaming (invoke) works fine. The gateway appears to decode the binary response body as UTF-8 text, replacing non-UTF-8 bytes with U+FFFD.

Reproduction

Minimal reproduction using @ai-sdk/amazon-bedrock and ai-gateway-provider:

import { createBedrockAnthropic } from "@ai-sdk/amazon-bedrock/anthropic"
import { createAiGateway } from "ai-gateway-provider"
import { streamText } from "ai"

// ❌ Broken: streaming through AI Gateway
const aigateway = createAiGateway({
  accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
  gateway: process.env.AI_GATEWAY_ID!,
  apiKey: process.env.AI_GATEWAY_API_KEY!,
})

const bedrockProvider = createBedrockAnthropic({
  region: "eu-central-1",
  apiKey: process.env.AWS_BEDROCK_API_KEY!, // Bedrock API Key (ABSK...)
})

const model = aigateway([
  bedrockProvider("eu.anthropic.claude-sonnet-4-6"),
])

const result = streamText({
  model,
  prompt: "Say hello",
})

// This will fail — the binary eventstream frames are corrupted
for await (const chunk of result.textStream) {
  process.stdout.write(chunk)
}
// ✅ Works: Bedrock directly (no AI Gateway)
const provider = createBedrockAnthropic({
  region: "eu-central-1",
  apiKey: process.env.AWS_BEDROCK_API_KEY!,
})

const result = streamText({
  model: provider("eu.anthropic.claude-sonnet-4-6"),
  prompt: "Say hello",
})

for await (const chunk of result.textStream) {
  process.stdout.write(chunk)
}

Root Cause

Bedrock streaming uses application/vnd.amazon.eventstream, a binary framing protocol. AI Gateway's proxy layer decodes the response body as UTF-8 text instead of passing through raw bytes. Non-UTF-8 bytes (e.g., 0x7A) become 0xEF 0xBF 0xBD (U+FFFD replacement character).

What works correctly:

  • Response headers are proxied correctly (content-type: application/vnd.amazon.eventstream, transfer-encoding: chunked)
  • AI Gateway logs show the full response with correct data
  • Non-streaming Bedrock calls (invoke) work fine

Environment

  • Region: eu-central-1
  • Model: eu.anthropic.claude-sonnet-4-6
  • Auth: Bedrock API key (Bearer token, no SigV4)
  • SDK: @ai-sdk/amazon-bedrock v4.x (Vercel AI SDK v6)
  • Gateway package: ai-gateway-provider

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions