# Streaming

URL: https://interfaze.ai/docs/streaming

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

## Text streaming

```tabs
language-typescript

tab-OpenAI SDK
const stream = await interfaze.chat.completions.create({
  model: "interfaze-beta",
  messages: [
    { role: "user", content: "Write a short story about a robot learning to paint" }
  ],
  stream: true
});

for await (const chunk of stream) {
  const content = chunk.choices[0]?.delta?.content;
  if (content) {
    process.stdout.write(content);
  }
}
tab-Vercel AI SDK
const { textStream } = await streamText({
  model: interfaze.chat('interfaze-beta'),
  prompt: "Write a short story about a robot learning to paint",
});

for await (const delta of textStream) {
  process.stdout.write(delta);
}
tab-LangChain SDK
import { ChatOpenAI } from "@langchain/openai";

const interfaze = new ChatOpenAI({
  configuration: {
    baseURL: "https://api.interfaze.ai/v1",
  },
  apiKey: "<your-api-key>",
  model: "interfaze-beta",
  streaming: true,
});

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);
}

language-python

tab-OpenAI SDK
stream = interfaze.chat.completions.create(
    model="interfaze-beta",
    messages=[{"role": "user", "content": "Write a short story about a robot learning to paint"}],
    stream=True
)

for chunk in stream:
    if not chunk.choices:
        continue
    delta = chunk.choices[0].delta
    if delta and delta.content is not None:
        print(delta.content, end="", flush=True)
tab-LangChain SDK
from langchain_openai import ChatOpenAI

interfaze = ChatOpenAI(
  base_url="https://api.interfaze.ai/v1",
  api_key="<your-api-key>",
  model="interfaze-beta",
  streaming=True
)

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

## Object streaming

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

```tabs
language-typescript

tab-OpenAI SDK
import { z } from "zod";
import { zodResponseFormat } from "openai/helpers/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.beta.chat.completions.stream({
  model: "interfaze-beta",
  messages: [
    { role: "user", content: "Write a short story about a robot learning to paint" }
  ],
  response_format: zodResponseFormat(storySchema, "story_schema"),
});

for await (const chunk of stream) {
  const partial = chunk.choices[0]?.delta?.content;
  if (partial) {
    process.stdout.write(partial);
  }
}

const finalObject = await stream.finalMessage();
console.log(JSON.parse(finalObject.choices[0].message.content));
tab-Vercel AI SDK
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.chat("interfaze-beta"),
  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);
}
tab-LangChain SDK
import { ChatOpenAI } from "@langchain/openai";
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 ChatOpenAI({
  configuration: {
    baseURL: "https://api.interfaze.ai/v1",
  },
  apiKey: "<your-api-key>",
  model: "interfaze-beta",
  streaming: true,
});

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);
}

language-python

tab-OpenAI SDK
from pydantic import BaseModel, Field
import json

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.create(
    model="interfaze-beta",
    messages=[{"role": "user", "content": "Write a short story about a robot learning to paint"}],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "story_schema",
            "schema": StorySchema.model_json_schema(),
        }
    },
    stream=True,
)

collected = ""
for chunk in stream:
    if not chunk.choices:
        continue
    delta = chunk.choices[0].delta
    if delta and delta.content is not None:
        collected += delta.content
        print(delta.content, end="", flush=True)

print(json.loads(collected))
tab-LangChain SDK
from langchain_openai import ChatOpenAI
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 = ChatOpenAI(
    base_url="https://api.interfaze.ai/v1",
    api_key="<your-api-key>",
    model="interfaze-beta",
    streaming=True
)

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)
```
