-
Notifications
You must be signed in to change notification settings - Fork 163
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
vertex[major]: upgrade pydantic #475
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
`_format_json_schema_to_gapic` translates json schema to gapic. It appears tailored to json schema as generated from Pydantic V1 BaseModels. Here we update this function to accommodate Pydantic V2 models. We keep the original function as `_format_json_schema_to_gapic_v1` and route V1 BaseModels through it. In Pydantic V1, an `Optional[int]` will be represented as an `integer` type that is not included in `required` fields. In Pydantic V2, it is represented as a union type (e.g., `{'anyOf': [{'type': 'integer'}, {'type': 'null'}], 'title': 'field_name'}` and included in `required` fields. Example: ```python from typing import Optional from pydantic.v1 import BaseModel as BaseModelV1 class PersonV1(BaseModelV1): name: str age: Optional[int] PersonV1.schema() ``` ``` {'title': 'PersonV1', 'type': 'object', 'properties': {'name': {'title': 'Name', 'type': 'string'}, 'age': {'title': 'Age', 'type': 'integer'}}, 'required': ['name']} ``` ```python from typing import Optional from pydantic import BaseModel class Person(BaseModel): name: str age: Optional[int] Person.model_json_schema() ``` ``` {'properties': {'name': {'title': 'Name', 'type': 'string'}, 'age': {'anyOf': [{'type': 'integer'}, {'type': 'null'}], 'title': 'Age'}}, 'required': ['name', 'age'], 'title': 'Person', 'type': 'object'} ``` `_format_json_schema_to_gapic` does not support this (`anyOf` is not in `_ALLOWED_SCHEMA_FIELDS_SET`). Here we add a condition for `anyOf`: if the `anyOf` consists of two elements, one of which is null, we simplify the schema to contain a single type and remove it from `required` (similar to Pydantic V1).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
In preparation for langchain 0.3 release
Todo: