docs/src/content/en/reference/ai-sdk/handle-network-stream.mdx
import PropertiesTable from "@site/src/components/PropertiesTable";
:::warning[Deprecated]
Agent networks are deprecated and will be removed in a future release. Use supervisor agents with agent.stream() or agent.generate() instead. See the migration guide to upgrade.
:::
Framework-agnostic handler for streaming network execution in AI SDK-compatible format. Use this function directly when you need to handle network streaming outside Hono or Mastra's own apiRoutes feature.
handleNetworkStream() returns a ReadableStream that you can wrap with createUIMessageStreamResponse().
handleNetworkStream() keeps the existing AI SDK v5/default behavior. If your app is typed against AI SDK v6, pass version: 'v6'.
Use networkRoute() if you want to create a network route inside a Mastra server.
Next.js App Router example:
import { handleNetworkStream } from '@mastra/ai-sdk'
import { createUIMessageStreamResponse } from 'ai'
import { mastra } from '@/src/mastra'
export async function POST(req: Request) {
const params = await req.json()
const stream = await handleNetworkStream({
mastra,
agentId: 'routingAgent',
params,
})
return createUIMessageStreamResponse({ stream })
}
<PropertiesTable
content={[
{
name: 'version',
type: "'v5' | 'v6'",
description:
"Selects the AI SDK stream contract to emit. Omit it or pass 'v5' for the existing default behavior. Pass 'v6' when your app is typed against AI SDK v6 response helpers.",
isOptional: true,
defaultValue: "'v5'",
},
{
name: 'mastra',
type: 'Mastra',
description: 'The Mastra instance to use for agent lookup and execution.',
isOptional: false,
},
{
name: 'agentId',
type: 'string',
description: 'The ID of the routing agent to execute as a network.',
isOptional: false,
},
{
name: 'params',
type: 'NetworkStreamHandlerParams',
description:
'The request parameters containing messages and execution options. Includes messages (required) and any AgentExecutionOptions like memory, maxSteps, runId, etc.',
isOptional: false,
},
{
name: 'defaultOptions',
type: 'AgentExecutionOptions',
description:
'Default options passed to agent execution. These are merged with params, with params taking precedence.',
isOptional: true,
},
]}
/>