-
Notifications
You must be signed in to change notification settings - Fork 138
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
genai: fix pydantic structured_output with array #469
genai: fix pydantic structured_output with array #469
Conversation
8408eb7
to
e0d26db
Compare
could you add a unit test too, please? |
e0d26db
to
41c1a2e
Compare
2f7e53d
to
2ecdfa3
Compare
@lkuligin
|
259c9d3
to
d25395f
Compare
return TYPE_ENUM[stype] | ||
else: | ||
pass | ||
return _get_type_from_str(stype) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we maybe just have a one-liner here instead creating a separate function?
TYPE_ENUM.get(stype, "str")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lkuligin
I fixed as one-liner.
Any progress on this? |
8ec641c
to
056cfdf
Compare
8d08df9
to
9212f29
Compare
030ee52
to
9451da9
Compare
Encountered this issue too |
model = ChatGoogleGenerativeAI(model=_MODEL, safety_settings=safety).bind_tools( | ||
[MyModel] | ||
) | ||
response = model.invoke([message]) | ||
print("response=", response) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nits: we probably don't need it anymore, do we?
Thanks for the contribution. I've noticed we still have issues when there's an array like this: class Person(BaseModel):
name: str = Field(..., description="The name of the person")
height_in_meters: float = Field(
..., description="The height of the person expressed in meters."
)
class People(BaseModel):
people: List[Person] So the following code raised some spec ignoring warning and the result leads to invalid Pydantic input: llm = ChatGoogleGenerativeAI(model='gemini-1.5-pro', api_key=settings.google_api_key, temperature=0.2)
query = "Anna is 23 years old and she is 6 feet tall; John is 34 and about 171cm"
prompt = ChatPromptTemplate.from_messages([("system", "Answer the user query."), ("human", "{query}") ])
chain = prompt | llm.with_structured_output(People, include_raw=True)
# WARNING OUTPUT:
# Value 'Information about a person.' is not supported in schema, ignoring v=Information about a person.
# Value '['name', 'height_in_meters']' is not supported in schema, ignoring v=['name', 'height_in_meters']
# Value 'Person' is not supported in schema, ignoring v=Person
# Value 'object' is not supported in schema, ignoring v=object
# Value 'Information about a person.' is not supported in schema, ignoring v=Information about a person.
# Value '['name', 'height_in_meters']' is not supported in schema, ignoring v=['name', 'height_in_meters']
# Value 'Person' is not supported in schema, ignoring v=Person
# Value 'object' is not supported in schema, ignoring v=object
chain.invoke({"query": query})
# OUTPUT:
# {'name': 'People', 'parameters': {'type_': 6, 'properties': {'people': {'type_': 5, 'items': {'type_': 1, 'format_': '', 'description': '', 'nullable': False, 'enum': [], 'max_items': '0', 'min_items': '0', 'properties': {}, 'required': []}, 'format_': '', 'description': '', 'nullable': False, 'enum': [], 'max_items': '0', 'min_items': '0', 'properties': {}, 'required': []}}, 'required': ['people'], 'format_': '', 'description': '', 'nullable': False, 'enum': [], 'max_items': '0', 'min_items': '0'}, 'description': ''}
# {'raw': AIMessage(content='', additional_kwargs={'function_call': {'name': 'People', 'arguments': '{"people": ["Anna", "John"]}'}}, response_metadata={'prompt_feedback': {'block_reason': 0, 'safety_ratings': []}, 'finish_reason': 'STOP', 'safety_ratings': [{'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_HATE_SPEECH', 'probability': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_HARASSMENT', 'probability': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability': 'NEGLIGIBLE', 'blocked': False}]}, id='run-d4f260cb-1ec2-49a9-befc-e99b8ba235de-0', tool_calls=[{'name': 'People', 'args': {'people': ['Anna', 'John']}, 'id': 'df592d4a-a4a7-48b3-a212-983a97e100d1', 'type': 'tool_call'}], usage_metadata={'input_tokens': 65, 'output_tokens': 16, 'total_tokens': 81}), 'parsing_error': 2 validation errors for People
# people.0
# Input should be a valid dictionary or instance of Person [type=model_type, input_value='Anna', input_type=str]
# For further information visit https://errors.pydantic.dev/2.8/v/model_type
# people.1
# Input should be a valid dictionary or instance of Person [type=model_type, input_value='John', input_type=str]
# For further information visit https://errors.pydantic.dev/2.8/v/model_type, 'parsed': None} Is it a Gemini limitation that I'm missing? |
PR Description
Fix pydantic structured_output with array
Relevant issues
#24225
langchain-ai/langchain#24225
Type
🐛 Bug Fix
Testing(optional)
This PR can pass like here.
Test result
Note1
This code shoud be updated later.
_set_schema_items() can't create correct function-calling scema.
https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/function-calling?hl#schema
Note2
This PR may be fix #492.
integration_test filed by this issue.