Skip to content

Error while streaming -> AnthropicError: Streaming is required for operations that may take longer than 10 minutes #664

@jacobokoenig

Description

@jacobokoenig

Hi, I'm trying to invoke a simple agent with structured outputs but I'm getting the following error:

AnthropicError: Streaming is required for operations that may take longer than 10 minutes.

I am indeed attempting to stream, but I get the error nonetheless:

// Initialize the Anthropic chat model
  const model = await initChatModel("claude-sonnet-4-5", {
    modelProvider: "anthropic",
    maxTokens: MAX_TOKENS,
    anthropicApiKey: process.env.CLAUDE_API_KEY,
  })

  // Create agent with structured output using provider strategy
  // Since Anthropic supports native structured output, we use providerStrategy
  const agent = createAgent({
    model,
    responseFormat: providerStrategy(schema),
  })

  // Use streaming to process the request, but collect all events until completion
  const stream = await agent.stream({
    messages: [
      {
        role: "user",
        content: prompt,
      },
    ],
  })

  // Collect all stream events until completion
  // The stream yields state updates, and the final state will contain structuredResponse
  let finalState: any = null
  for await (const event of stream) {
    console.log("stream event:", JSON.stringify(event, null, 2))
    // Each event is a state update - keep the latest one
    finalState = event
  }

  // Extract the structured response from the final state
  if (finalState?.structuredResponse) {
    return finalState.structuredResponse
  }

  // Fallback: if structuredResponse is not available, throw an error
  throw new Error("Structured response not found in agent result. The model may not have generated a valid structured output.")

Can anyone either pinpoint something I'm doing wrong or clarify if this is potentially a bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions