streamText
Stream a text response from a Foundry language model as it is generated.
import { streamText } from "@osdk/aip-core";
function streamText<TOOLS extends ToolSet = ToolSet>(
options: StreamTextOptions<TOOLS>,
): StreamTextResult;Starts the request immediately and returns streams and promises without awaiting. Read textStream for text only, or fullStream for every chunk, including reasoning, tool calls, and the finish event. Like generateText, the call is single-step.
Options
Prop
Type
Pass either prompt or messages, not both. streamText throws synchronously when both are set, or when system, prompt, and messages are all empty. Aborting abortSignal stops the request. Cancelling the streams stops it only when you cancel both fullStream and textStream.
Returns
Prop
Type
On failure, fullStream emits { type: "error", error } and then errors, and the promises reject, except warnings, which still resolves.
TextStreamChunk
type | Fields |
|---|---|
"text-delta" | id, delta |
"reasoning-delta" | id, delta |
"tool-call" | toolCallId, toolName, input |
"finish" | finishReason, rawFinishReason, usage |
"error" | error |
Example
import { foundryModel, streamText } from "@osdk/aip-core";
import type { PlatformClient } from "@osdk/client";
export async function streamAnswer(
client: PlatformClient,
question: string,
onText: (delta: string) => void,
) {
const result = streamText({
model: foundryModel({ client, model: "gpt-4o" }),
prompt: question,
});
const reader = result.textStream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
onText(value);
}
return result.finishReason;
}