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

Raw response is not present when the response model is an Iterable #1305

Open
jacobtohahn opened this issue Jan 13, 2025 Discussed in #365 · 1 comment
Open

Raw response is not present when the response model is an Iterable #1305

jacobtohahn opened this issue Jan 13, 2025 Discussed in #365 · 1 comment
Labels
bug Something isn't working

Comments

@jacobtohahn
Copy link

Discussed in #365

Originally posted by chrishiste January 25, 2024
Describe the bug
When using the instructor library, there seems to be an issue with the raw response not being available when the response model is an Iterable.

To Reproduce

from typing import Iterable

import instructor
from openai import OpenAI
from pydantic import BaseModel


client = instructor.patch(OpenAI())


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


users = client.chat.completions.create(
    model="gpt-3.5-turbo",
    response_model=Iterable[UserDetail],
    messages=[
        {"role": "user", "content": "Give me 3 users"},
    ],
)

print(users) # [UserDetail(name='Alice', age=25), UserDetail(name='Bob', age=30), UserDetail(name='Charlie', age=35)]
assert users._raw_response is not None # AttributeError: 'list' object has no attribute '_raw_response'

Expected behavior
The expected behavior is that the raw response should be available and accessible.

Extracting Tasks using Iterable in the docs makes it seem as if this is supposed to work in general with models (regardless of if they also include the raw response).

The issue happens due to create_with_completion() in client.py attempting to access _raw_response when the response_model is an Iterable (i.e., a list), resulting in the error AttributeError: 'list' object has no attribute '_raw_response'.

@github-actions github-actions bot added the bug Something isn't working label Jan 13, 2025
@jacobtohahn
Copy link
Author

jacobtohahn commented Jan 13, 2025

For now a viable workaround is to create a class to hold the Iterable:

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


class Users(BaseModel):
    users: List[User]
    
# Instead of response_model = List[User], use:
# response_model = Users

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

No branches or pull requests

1 participant