Skip to content

Rework models api#55

Merged
anbuzin merged 2 commits into
mainfrom
fix-models
Apr 29, 2026
Merged

Rework models api#55
anbuzin merged 2 commits into
mainfrom
fix-models

Conversation

@anbuzin

@anbuzin anbuzin commented Apr 28, 2026

Copy link
Copy Markdown
Collaborator
  1. implement stream context manager
  2. make messages and parts mutable
  3. move message aggregation from individual adapters into ai.stream
  4. simplify adapters to emit only streaming events
  5. temporarily patch agents and middleware for the new api

@vercel

vercel Bot commented Apr 28, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
py-ai Ready Ready Preview, Comment Apr 29, 2026 0:00am

Request Review

1. implement stream context manager
2. make messages and parts mutable
3. move message aggregation from individual adapters into ai.stream
4. simplify adapters to emit only streaming events
5. temporarily patch agents and middleware for the new api
Comment thread src/ai/agents/agent.py

# Bridge: emit MessageStart/MessageEnd around the assistant message
# the model stream just produced, so _collect_messages and downstream
# consumers (AI-SDK outbound, label stamping) see the same boundary

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(what is AI-SDK outbound?)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The part of the AI SDK UI adapter (the one that has useChat) responsible for converting the stream from backend to frontend format

Comment thread src/ai/agents/runtime.py
from collections.abc import AsyncGenerator, AsyncIterable, Awaitable

from .. import types
from . import events as events_

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the underscore? Because there are lots of variables named events?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's at least one somewhere in there

@anbuzin anbuzin merged commit ae06278 into main Apr 29, 2026
6 checks passed
@anbuzin anbuzin deleted the fix-models branch April 29, 2026 16:08
msullivan added a commit that referenced this pull request Apr 29, 2026
Gateway will both stream a tool call and provide a full tool-call
event after, which leads to duplicate tool calls in the stream, which
then blow up in the validator.

This duplication in the stream existed previously, but changes in how
the stream is assembled in #55 cause it to break tool calling in the
agent (no longer using an id-keyed dict to build up the parts).

Fix it at the ai-gateway parsing level.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants