genai: Fixed nested pydantic structures recursion #658
+237
−5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Description
While working with gregpr07/browser-use, @MahlerTom and I found several bugs in langchain handling of nested
Pydantic
structured inwith_structured_output
. Specifically, the issues occurred in_get_properties_from_schema
method inlibs\genai\langchain_google_genai\_function_utils.py
.glm.Type.OBJECT
and only took care of the first layer.pydantic
fields are required (no default value is provided), the method did not properly add these fields to therequired
list.glm.Type.OBJECT
had empty properties, there was an error that properties of typeglm.Type.OBJECT
must not be empty.Relevant issues
Fixes #657,
May be related to langchain-ai/langchain#24225
Type
🐛 Bug Fix
Changes
As mentioned above:
glm.Type.OBJECT
and only took care of the first layer. We fixed the recursion with:However, we found that when sending nested
pedantic
objects with required fields as expected output structure, like the case in the following method from gregpr07/browser-use below:Here, when we observed the
response['parsed']
was oftenNone
andresponse['parsing_error']
included validation errors for missing fields that were required.required
list when parsing thepydantic
objects. We added the nestedif
to our fix:But still got this error:
This was because in cases where an object of type
glm.Type.OBJECT
had empty properties, as was the case with the 'go_back' action in gregpr07/browser-use, there was an error that properties of typeglm.Type.OBJECT
must not be empty. We changed that by adding dummy type in such cases adding the lastelse
statement:Testing
Tested on the code described in #657
Note