Theo Docs
TheoKitTheoKit-SDKTheoUITheo PaaS
Concepts

Streaming

Read tokens, tool calls, and artifacts as they arrive — AsyncIterator of SDKMessage.

Canonical contract: packages/sdk/docs.mdSDKMessage discriminated union, run.stream(), Vercel AI Data Stream v1 wire format.

agent.send(prompt) returns a Run. You can await run.wait() for the full result, OR iterate run.stream() to consume events as they happen.

Iterate the stream

const run = await agent.send("Tell me a story about robots.");

for await (const msg of run.stream()) {
  switch (msg.type) {
    case "text_delta":
      process.stdout.write(msg.text);
      break;
    case "tool_call":
      console.log("[calling tool]", msg.name);
      break;
    case "tool_result":
      console.log("[tool returned]", msg.content);
      break;
    case "finished":
      console.log("\n[done]", msg.stopReason);
      break;
  }
}

SDKMessage is a discriminated union. The full variant list:

typeMeaning
text_deltaIncremental text token from the model
tool_callModel emitted a tool call (name + args)
tool_resultTool execution finished (success or error)
partial_objectWhen using streamObject, partial typed payload
complete_objectWhen using streamObject, final typed payload
finishedRun is over (with `stopReason: end_turn
errorA non-recoverable error was emitted

Structured streaming (streamObject)

For typed payloads, use Agent.streamObject<T>:

import { z } from "zod";
const schema = z.object({ title: z.string(), bullets: z.array(z.string()) });

const stream = await Agent.streamObject({
  apiKey, model, schema,
  prompt: "Outline a blog post about Rust ownership.",
});

for await (const msg of stream) {
  if (msg.type === "partial_object") console.log("partial:", msg.value);
  if (msg.type === "complete_object") console.log("final:", msg.value);
}

The SDK uses a synthetic forced tool (ADR D33) — the model calls a single tool whose schema is your Zod schema, the SDK validates each partial, and you get progressively-typed updates.

React hooks

For browser apps, @theokit/react exposes hooks that consume the same wire format (Vercel AI Data Stream v1, ADR D38):

  • useTheoChat — multi-turn chat
  • useTheoCompletion — single-shot text
  • useTheoAssistant<T> — object streaming

See the react-nextjs example.

API reference

On this page