# Streaming

URL: https://interfaze-landing-1am6r0242-jigsaw-stack.vercel.app/docs/streaming

Streaming enables Interfaze to deliver responses in real time, creating a faster, more interactive
experience.

## Text streaming

**Interfaze SDK · typescript**

```typescript
const stream = interfaze.chat.completions.stream({
  messages: [
    { role: "user", content: "Write a short story about a robot learning to paint" }
  ],
});

for await (const text of stream.textDeltas()) {
  process.stdout.write(text);
}

// the final completion still carries precontext and reasoning
const final = await stream.finalChatCompletion();
```

**Vercel AI SDK · typescript**

```typescript
const { textStream } = streamText({
  model: interfaze('interfaze-1'),
  prompt: "Write a short story about a robot learning to paint",
});

for await (const delta of textStream) {
  process.stdout.write(delta);
}
```

**LangChain SDK · typescript**

```typescript
import { ChatInterfaze } from "@interfaze-ai/langchain";

const interfaze = new ChatInterfaze({
  apiKey: process.env.INTERFAZE_API_KEY,
});

const stream = await interfaze.stream(
  "Write a short story about a robot learning to paint"
);

for await (const chunk of stream) {
  process.stdout.write(chunk.content as string);
}
```

**Interfaze SDK · python**

```python
stream = interfaze.chat.completions.stream(
    messages=[{"role": "user", "content": "Write a short story about a robot learning to paint"}],
)

for text in stream.text_deltas():
    print(text, end="", flush=True)

# the final completion still carries precontext and reasoning
final = stream.get_final_completion()
```

**LangChain SDK · python**

```python
import os
from interfaze_langchain import ChatInterfaze

interfaze = ChatInterfaze(
  api_key=os.environ["INTERFAZE_API_KEY"]
)

for chunk in interfaze.stream("Write a short story about a robot learning to paint"):
  print(chunk.content, end="", flush=True)
```

- `textDeltas()` yields display-ready text — the inline `<precontext>` and `<think>` side-channels are stripped out and returned structured on the final completion instead.
- For the raw chunk iterator, use `create({ stream: true })`.
- The native [LangChain integration](https://interfaze-landing-1am6r0242-jigsaw-stack.vercel.app/docs/integrations/langchain-sdk) strips the same side-channels out of streamed content. Set `showAdditionalInfo` (`show_additional_info` in Python) on the model to receive `precontext` while streaming.

## Object streaming

Stream structured JSON objects as they are generated, receiving partial data incrementally rather than waiting for the full response.

**Interfaze SDK · typescript**

```typescript
import { responseFormat } from "interfaze";
import { z } from "zod";

const storySchema = z.object({
  title: z.string().describe("The title of the story"),
  genre: z.string().describe("The genre of the story"),
  summary: z.string().describe("A brief summary of the story"),
});

const stream = interfaze.chat.completions.stream({
  messages: [
    { role: "user", content: "Write a short story about a robot learning to paint" }
  ],
  response_format: responseFormat(z.toJSONSchema(storySchema), "story_schema"),
});

for await (const partial of stream.textDeltas()) {
  process.stdout.write(partial);
}

const final = await stream.finalChatCompletion();
console.log(JSON.parse(final.choices[0]?.message.content ?? "{}"));
```

**Vercel AI SDK · typescript**

```typescript
import { streamText, Output } from "ai";
import { z } from "zod";

const storySchema = z.object({
  title: z.string().describe("The title of the story"),
  genre: z.string().describe("The genre of the story"),
  summary: z.string().describe("A brief summary of the story"),
});

const { partialOutputStream } = streamText({
  model: interfaze("interfaze-1"),
  output: Output.object({ schema: storySchema }),
  prompt: "Write a short story about a robot learning to paint",
});

for await (const partial of partialOutputStream) {
  console.clear();
  console.log(partial);
}
```

**LangChain SDK · typescript**

```typescript
import { ChatInterfaze } from "@interfaze-ai/langchain";
import { z } from "zod";

const storySchema = z.object({
  title: z.string().describe("The title of the story"),
  genre: z.string().describe("The genre of the story"),
  summary: z.string().describe("A brief summary of the story"),
});

const interfaze = new ChatInterfaze({
  apiKey: process.env.INTERFAZE_API_KEY,
});

const structuredModel = interfaze.withStructuredOutput(storySchema);

const stream = await structuredModel.stream(
  "Write a short story about a robot learning to paint"
);

for await (const chunk of stream) {
  console.log(chunk);
}
```

**Interfaze SDK · python**

```python
import json
from interfaze import response_format
from pydantic import BaseModel, Field

class StorySchema(BaseModel):
    title: str = Field(..., description="The title of the story")
    genre: str = Field(..., description="The genre of the story")
    summary: str = Field(..., description="A brief summary of the story")

stream = interfaze.chat.completions.stream(
    messages=[{"role": "user", "content": "Write a short story about a robot learning to paint"}],
    response_format=response_format(StorySchema.model_json_schema(), "story_schema"),
)

for partial in stream.text_deltas():
    print(partial, end="", flush=True)

final = stream.get_final_completion()
print(json.loads(final.choices[0].message.content or "{}"))
```

**LangChain SDK · python**

```python
import os
from interfaze_langchain import ChatInterfaze
from pydantic import BaseModel, Field

class StorySchema(BaseModel):
    title: str = Field(..., description="The title of the story")
    genre: str = Field(..., description="The genre of the story")
    summary: str = Field(..., description="A brief summary of the story")

interfaze = ChatInterfaze(
    api_key=os.environ["INTERFAZE_API_KEY"]
)

structured_model = interfaze.with_structured_output(StorySchema)

for chunk in structured_model.stream("Write a short story about a robot learning to paint"):
    print(chunk)
```
