Raw response is not present when the response model is an Iterable
#365
Replies: 8 comments
-
I was just about to file another issue related to this. I think an instructor.patch-ed client should always return a wrapper object (let's call it) This would allow a user to fall back to other strategies or have debug metadata if the Pydantic casting failed. Currently it's a bit of a take-it-all or leave-it. |
Beta Was this translation helpful? Give feedback.
-
Your question got me digging and help me put some pieces together about instructor. Your error makes sense because at high level this line from typing import List
import instructor
from openai import OpenAI
from pydantic import BaseModel
client = instructor.patch(
OpenAI()
)
class UserDetail(BaseModel):
name: str
age: int
class UserDetailList(BaseModel):
users: List[UserDetail]
users = client.chat.completions.create(
model="gpt-3.5-turbo",
response_model=UserDetailList,
messages=[
{"role": "user", "content": "Give me 3 users"},
],
)
print(users._raw_response)
# ChatCompletion(id='chatcmpl-8lBAU77cgwRjc3u5cvlH050m9TEAK', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content=None, role='assistant', function_call=FunctionCall(arguments='{\n "users": [\n { "name": "John", "age": 25 },\n { "name": "Emily", "age": 30 },\n { "name": "Michael", "age": 35 }\n ]\n}', name='UserDetailList'), tool_calls=None))], created=1706255038, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=51, prompt_tokens=80, total_tokens=131)) The _raw_response is tied to the parent model UserDetailList not the child model UserDetail. Judging by all his examples though this is the intent. I could be wrong but this was my understanding. Hope that's helpful. |
Beta Was this translation helpful? Give feedback.
-
@Tedfulk I think it would make a lot of sense to try to provide the raw response in some form (see my suggestion above) -- there is valuable metadata in there. |
Beta Was this translation helpful? Give feedback.
-
@indigoviolet sorry I didn't see your reply in time before I posted. Hmm that does sound nice. I basically just use _raw_response to check out responses coming back or token limit, and do another round of validation if need be. Although pydantic and insructor do a good job of validating the llms response, or errors. Usually cuz the open source models cant figure out json. Idk I'm still leaning towards how they are now by having the response model be a specific model I can expect to be returned with a hard pattern to adhere to. |
Beta Was this translation helpful? Give feedback.
-
@Tedfulk I'm using the same workaround at the moment but it requires more boilerplate. Would be nice if the returned value was always an object with the default value correctly typed to the |
Beta Was this translation helpful? Give feedback.
-
Yeah, the iterable type hint really is just to help when |
Beta Was this translation helpful? Give feedback.
-
Unfortunately using the parent model workaround which is not of type |
Beta Was this translation helpful? Give feedback.
-
I'm running into the same issue as @chrishiste. For now, I'm going to workaround using a parent model as described. That being said, 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), so this probably is a bug. Is there any update on a fix besides the workaround? |
Beta Was this translation helpful? Give feedback.
-
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
Expected behavior
The expected behavior is that the raw response should be available and accessible.
Beta Was this translation helpful? Give feedback.
All reactions