Skip to content

Conversation

@cpsievert
Copy link
Collaborator

Summary

  • Adds data_model parameter to stream() and stream_async() methods
  • When provided, constrains the response format to match a Pydantic model
  • Streamed chunks are JSON text that can be parsed after consuming the stream

Usage

from chatlas import ChatOpenAI
from pydantic import BaseModel

class Person(BaseModel):
    name: str
    age: int

chat = ChatOpenAI()
chunks = list(chat.stream("John is 25 years old", data_model=Person))
person = Person.model_validate_json("".join(chunks))

Test plan

  • Added test_stream_with_data_model() test
  • Added test_stream_async_with_data_model() test
  • All 450 tests pass
  • Type checking passes

🤖 Generated with Claude Code

@cpsievert cpsievert force-pushed the feat/stream-data-model branch 2 times, most recently from c1cb123 to efbc21c Compare January 6, 2026 16:03
@cpsievert cpsievert changed the title feat: add data_model support to stream() and stream_async() methods feat: add data_model support to .stream() and .stream_async() methods Jan 6, 2026
@cpsievert cpsievert force-pushed the feat/stream-data-model branch from efbc21c to 80a10c9 Compare January 6, 2026 16:05
Enable structured data extraction while streaming responses. The data_model
parameter constrains the response format to match a Pydantic model. Users can
parse the accumulated chunks with model.model_validate_json("".join(chunks)).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@cpsievert cpsievert force-pushed the feat/stream-data-model branch from 80a10c9 to 2ef78c0 Compare January 6, 2026 16:24
@cpsievert cpsievert merged commit b32db90 into main Jan 6, 2026
9 checks passed
@cpsievert cpsievert deleted the feat/stream-data-model branch January 6, 2026 16:30
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