Skip to content

Using | breaks with partial streaming #1523

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

Open
1 task
ahuang11 opened this issue May 13, 2025 · 0 comments
Open
1 task

Using | breaks with partial streaming #1523

ahuang11 opened this issue May 13, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@ahuang11
Copy link

  • This is actually a bug report.

What Model are you using?

gpt-4o-mini

Describe the bug

Follow up on #661 since that was closed before it was fixed

When streaming with a |, it crashes with TypeError: type 'types.UnionType' is not subscriptable

To Reproduce

import instructor
from pydantic import BaseModel
from openai import OpenAI


class User(BaseModel):
    name: str
    age: int
    category: str | None

client = instructor.from_openai(OpenAI())

# Extract structured data from natural language
res = client.chat.completions.create_partial(
    model="gpt-4o-mini",
    response_model=User,
    messages=[{"role": "user", "content": "John Doe is 30 years old."}],
)
res

Expected behavior

Swapping to Union works, but I expect them both to work

import instructor
from pydantic import BaseModel
from openai import OpenAI


class User(BaseModel):
    name: str
    age: int
    category: Union[str, None]

client = instructor.from_openai(OpenAI())

# Extract structured data from natural language
res = client.chat.completions.create_partial(
    model="gpt-4o-mini",
    response_model=User,
    messages=[{"role": "user", "content": "John Doe is 30 years old."}],
)
res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant