Class References
Function References
- text
- textOptions
- combineStrategies
- convertMessagesToModelMessages
- convertZodToJsonSchema
- createReplayStream
- embedding
- generateMessageId
- maxIterations
- messages
- modelMessageToUIMessage
- modelMessagesToUIMessages
- normalizeToUIMessage
- parsePartialJSON
- summarize
- toServerSentEventsStream
- toServerSentEventsResponse
- toolDefinition
- uiMessageToModelMessages
- realtimeToken
- untilFinishReason
Interface References
- AIAdapter
- AIAdapterConfig
- AgentLoopState
- AudioPart
- AudioVisualization
- BaseStreamChunk
- TextCompletionChunk
- TextOptions
- ChunkRecording
- ChunkStrategy
- ClientTool
- ContentPartSource
- ContentStreamChunk
- DefaultMessageMetadataByModality
- DocumentPart
- DoneStreamChunk
- EmbeddingOptions
- EmbeddingResult
- ErrorStreamChunk
- ImagePart
- InternalToolCallState
- JSONParser
- ModelMessage
- ProcessorResult
- ProcessorState
- RealtimeAdapter
- RealtimeClientOptions
- RealtimeConnection
- RealtimeMessage
- RealtimeSessionConfig
- RealtimeToken
- RealtimeTokenAdapter
- RealtimeTokenOptions
- ResponseFormat
- ServerTool
- StreamProcessorEvents
- StreamProcessorHandlers
- StreamProcessorOptions
- SummarizationOptions
- SummarizationResult
- TextPart
- ThinkingPart
- ThinkingStreamChunk
- Tool
- ToolCall
- ToolCallPart
- ToolCallStreamChunk
- ToolConfig
- ToolDefinition
- ToolDefinitionConfig
- ToolDefinitionInstance
- ToolInputAvailableStreamChunk
- ToolResultPart
- ToolResultStreamChunk
- UIMessage
- VideoPart
Type Alias References
- AgentLoopStrategy
- AnyClientTool
- TextStreamOptionsForModel
- TextStreamOptionsUnion
- ConstrainedContent
- ConstrainedModelMessage
- ContentPart
- ContentPartForModalities
- ExtractModalitiesForModel
- ExtractModelsFromAdapter
- InferToolInput
- InferToolName
- InferToolOutput
- MessagePart
- RealtimeEvent
- RealtimeMessagePart
- RealtimeMode
- RealtimeStatus
- ModalitiesArrayToUnion
- Modality
- StreamChunk
- ToolCallState
- ToolResultState
Variable References
toJsonResponse
Call Signature#
function toJsonResponse(result, init?): Promise<Response>;Defined in: packages/ai/src/stream-to-response.ts:1684
Send a chat run as one JSON body, for hosts that cannot stream a response. Pair it with the fetchJson connection adapter.
The body is { chunks, offset?, done }:
- chunks: every chunk, encoded for the wire like the SSE and NDJSON helpers.
- offset: the durable offset of the last chunk (durable routes only).
- done: false when the run is still going. The client asks again with GET ?runId=<id>&offset=<offset>, served by resumeJsonResponse.
Pass a ChatResult from chat({ stream: false }) to send it as is. Pass the object from chat({ outputSchema }) to send it as a complete structured output. A Promise of either is awaited. Pass a stream to read it here. Without durability the stream is read to its end. With durability, a fresh run fills the log, and the reply goes out when the run ends or when maxWaitMs passes, whichever is first. The run keeps going after an early reply. A request with a resume offset reads the log instead.
Parameters#
result#
init?#
ResponseInit
Response init. For a stream, also abortController, signal, maxWaitMs, durability, and debug.
Returns#
Promise<Response>
A promise of a JSON Response
Example#
// A finished result
export async function POST(request: Request) {
const result = await chat({
adapter: openaiText('gpt-5.5'),
messages: [...],
stream: false,
});
return toJsonResponse(result);
}
// A structured output
export async function POST(request: Request) {
const person = await chat({
adapter: openaiText('gpt-5.5'),
messages: [...],
outputSchema: PersonSchema,
});
return toJsonResponse(person);
}
// A durable stream that replies at least once a second
export async function POST(request: Request) {
const stream = chat({ adapter: openaiText('gpt-5.5'), messages: [...] });
return toJsonResponse(stream, {
durability: { adapter: memoryStream(request) },
signal: request.signal,
maxWaitMs: 1000,
});
}Call Signature#
function toJsonResponse<TOffset>(stream, init?): Promise<Response>;Defined in: packages/ai/src/stream-to-response.ts:1688
Send a chat run as one JSON body, for hosts that cannot stream a response. Pair it with the fetchJson connection adapter.
The body is { chunks, offset?, done }:
- chunks: every chunk, encoded for the wire like the SSE and NDJSON helpers.
- offset: the durable offset of the last chunk (durable routes only).
- done: false when the run is still going. The client asks again with GET ?runId=<id>&offset=<offset>, served by resumeJsonResponse.
Pass a ChatResult from chat({ stream: false }) to send it as is. Pass the object from chat({ outputSchema }) to send it as a complete structured output. A Promise of either is awaited. Pass a stream to read it here. Without durability the stream is read to its end. With durability, a fresh run fills the log, and the reply goes out when the run ends or when maxWaitMs passes, whichever is first. The run keeps going after an early reply. A request with a resume offset reads the log instead.
Type Parameters#
TOffset#
TOffset extends string = string
Parameters#
stream#
AsyncIterable<AGUIEvent>
init?#
JsonResponseInit<TOffset>
Response init. For a stream, also abortController, signal, maxWaitMs, durability, and debug.
Returns#
Promise<Response>
A promise of a JSON Response
Example#
// A finished result
export async function POST(request: Request) {
const result = await chat({
adapter: openaiText('gpt-5.5'),
messages: [...],
stream: false,
});
return toJsonResponse(result);
}
// A structured output
export async function POST(request: Request) {
const person = await chat({
adapter: openaiText('gpt-5.5'),
messages: [...],
outputSchema: PersonSchema,
});
return toJsonResponse(person);
}
// A durable stream that replies at least once a second
export async function POST(request: Request) {
const stream = chat({ adapter: openaiText('gpt-5.5'), messages: [...] });
return toJsonResponse(stream, {
durability: { adapter: memoryStream(request) },
signal: request.signal,
maxWaitMs: 1000,
});
}Call Signature#
function toJsonResponse(structuredOutput, init?): Promise<Response>;Defined in: packages/ai/src/stream-to-response.ts:1692
Send a chat run as one JSON body, for hosts that cannot stream a response. Pair it with the fetchJson connection adapter.
The body is { chunks, offset?, done }:
- chunks: every chunk, encoded for the wire like the SSE and NDJSON helpers.
- offset: the durable offset of the last chunk (durable routes only).
- done: false when the run is still going. The client asks again with GET ?runId=<id>&offset=<offset>, served by resumeJsonResponse.
Pass a ChatResult from chat({ stream: false }) to send it as is. Pass the object from chat({ outputSchema }) to send it as a complete structured output. A Promise of either is awaited. Pass a stream to read it here. Without durability the stream is read to its end. With durability, a fresh run fills the log, and the reply goes out when the run ends or when maxWaitMs passes, whichever is first. The run keeps going after an early reply. A request with a resume offset reads the log instead.
Parameters#
structuredOutput#
object
init?#
ResponseInit
Response init. For a stream, also abortController, signal, maxWaitMs, durability, and debug.
Returns#
Promise<Response>
A promise of a JSON Response
Example#
// A finished result
export async function POST(request: Request) {
const result = await chat({
adapter: openaiText('gpt-5.5'),
messages: [...],
stream: false,
});
return toJsonResponse(result);
}
// A structured output
export async function POST(request: Request) {
const person = await chat({
adapter: openaiText('gpt-5.5'),
messages: [...],
outputSchema: PersonSchema,
});
return toJsonResponse(person);
}
// A durable stream that replies at least once a second
export async function POST(request: Request) {
const stream = chat({ adapter: openaiText('gpt-5.5'), messages: [...] });
return toJsonResponse(stream, {
durability: { adapter: memoryStream(request) },
signal: request.signal,
maxWaitMs: 1000,
});
}