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

Streamed list of Unions of BaseModels results in ValidationError #1140

Open
3 of 8 tasks
raghavpillai opened this issue Nov 4, 2024 · 2 comments
Open
3 of 8 tasks

Comments

@raghavpillai
Copy link

raghavpillai commented Nov 4, 2024

  • This is actually a bug report.
  • I am not getting good LLM Results
  • I have tried asking for help in the community on discord or discussions and have not received a response.
  • I have tried searching the documentation and have not found an answer.

What Model are you using?

  • gpt-3.5-turbo
  • gpt-4-turbo
  • gpt-4
  • Other (Claude 3.5 Sonnet)

Describe the bug
When trying to output list[Union[BaseModelA, BaseModelB] in a streamed response, the BaseModels do not become Partial models like they're supposed to. This results in a validation error when reading from the stream.

To Reproduce

import asyncio
import os
from typing import Union

import instructor
from pydantic import BaseModel, Field


class Highway(BaseModel):
    name: str = Field(
        ...,
        description="The name of the highway.",
    )


class Car(BaseModel):
    name: str = Field(
        ...,
        description="The name of the car.",
    )


class Exit(BaseModel):
    name: str = Field(
        ...,
        description="The name of the exit.",
    )


class PopulatedRoad(BaseModel):
    steps: list[Union[Exit, Car]] = Field(
        ...,
        description="The steps of the road.",
    )


if __name__ == "__main__":

    async def main():
        from anthropic import AsyncAnthropic

        async_client = instructor.from_anthropic(
            AsyncAnthropic(
                api_key=os.getenv("ANTHROPIC_API_KEY"),
            ),
            mode=instructor.Mode.ANTHROPIC_JSON,
        )

        response_gen = async_client.messages.create_partial(
            system="You are a helpful assistant that creates populated roads.",
            messages=[
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "text",
                            "text": "Create a populated road. Has to have at least 10 unique steps.",
                        }
                    ],
                }
            ],
            model="claude-3-5-sonnet-20241022",
            response_model=PopulatedRoad,
            max_tokens=2000,
            stream=True,
        )

        async for chunk in response_gen:
            print(chunk)

    asyncio.run(main())

Expected behavior
image

Current Behavior
image

@KanishkGar
Copy link

+1, I ran into this too

@degtrdg
Copy link

degtrdg commented Nov 14, 2024

+1, had the same issue

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 a pull request may close this issue.

3 participants