Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support accumulated/partial streamed objects #420

Open
jackmpcollins opened this issue Feb 2, 2025 · 0 comments
Open

Support accumulated/partial streamed objects #420

jackmpcollins opened this issue Feb 2, 2025 · 0 comments

Comments

@jackmpcollins
Copy link
Owner

Add a new Partial class that enables accumulation of a streamed pydantic model or list/set/str/etc i.e. returns the partially received object repeatedly until the stream is complete.

Use pydantic's support for partial parsing. See https://docs.pydantic.dev/latest/concepts/experimental/#partial-validation

from magentic import prompt, Partial
from pydantic import BaseModel, Field


class Superhero(BaseModel):
    name: str
    age: int | None = None
    power: str | None = None
    enemies: list[str] = Field(default_factory=list)


@prompt("Create a Superhero named {name}.")
def create_superhero(name: str) -> Partial[Superhero]: ...


for hero in create_superhero_team("Food Dude"):
    print(hero)
# Superhero()
# Superhero(name="Food")
# Superhero(name="Food Dude")
# Superhero(name="Food Dude", age=99)
# ...

Instructor has this concept https://python.useinstructor.com/concepts/partial/

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

No branches or pull requests

1 participant