Skip to content

Commit b46550d

Browse files
committed
Fix the typing issue and clean up the FastAPI Vite example
1 parent 240eb3a commit b46550d

3 files changed

Lines changed: 6 additions & 16 deletions

File tree

examples/fastapi-vite/backend/routes/chat.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from __future__ import annotations
44

5-
from collections.abc import AsyncGenerator
6-
75
from fastapi import APIRouter
86
from fastapi.responses import StreamingResponse
97
from pydantic import BaseModel
@@ -30,14 +28,6 @@ class ChatRequest(BaseModel):
3028
session_id: str | None = None
3129

3230

33-
async def _iter_result(
34-
result: ai.RunResult,
35-
) -> AsyncGenerator[ai.Message, None]:
36-
"""Unwrap RunResult into an AsyncGenerator for to_sse_stream."""
37-
async for msg in result:
38-
yield msg
39-
40-
4131
@router.post("/chat")
4232
async def chat(request: ChatRequest):
4333
"""Handle chat requests and stream responses."""
@@ -57,7 +47,7 @@ async def chat(request: ChatRequest):
5747
result = ai.run(graph, llm, messages, TOOLS, checkpoint=checkpoint)
5848

5949
async def stream_response():
60-
async for chunk in to_sse_stream(_iter_result(result)):
50+
async for chunk in to_sse_stream(result):
6151
yield chunk
6252

6353
# If the run completed (no pending hooks), clear the checkpoint

examples/fastapi-vite/backend/uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/vercel_ai_sdk/ai_sdk_ui/adapter.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import dataclasses
88
import json
99
import uuid
10-
from collections.abc import AsyncGenerator
10+
from collections.abc import AsyncGenerator, AsyncIterable
1111
from typing import Any, Literal
1212

1313
from .. import core
@@ -127,7 +127,7 @@ def begin_message(
127127

128128

129129
async def to_ui_message_stream(
130-
messages: AsyncGenerator[core.messages.Message, None],
130+
messages: AsyncIterable[core.messages.Message],
131131
) -> AsyncGenerator[protocol.UIMessageStreamPart, None]:
132132
"""
133133
Convert a proto_sdk message stream into AI SDK UI message stream parts.
@@ -257,7 +257,7 @@ async def to_ui_message_stream(
257257

258258

259259
async def filter_by_label(
260-
messages: AsyncGenerator[core.messages.Message, None],
260+
messages: AsyncIterable[core.messages.Message],
261261
label: str | None = None,
262262
) -> AsyncGenerator[core.messages.Message, None]:
263263
"""Filter a message stream to a single agent label.
@@ -273,7 +273,7 @@ async def filter_by_label(
273273

274274

275275
async def to_sse_stream(
276-
messages: AsyncGenerator[core.messages.Message, None],
276+
messages: AsyncIterable[core.messages.Message],
277277
) -> AsyncGenerator[str, None]:
278278
"""Convert a proto_sdk message stream directly into SSE-formatted strings."""
279279
async for part in to_ui_message_stream(messages):

0 commit comments

Comments
 (0)