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

vertex[major]: upgrade pydantic #475

Merged
merged 37 commits into from
Sep 11, 2024
Merged

vertex[major]: upgrade pydantic #475

merged 37 commits into from
Sep 11, 2024

Conversation

ccurme
Copy link
Contributor

@ccurme ccurme commented Sep 5, 2024

In preparation for langchain 0.3 release

Todo:

  • fix gapic format test + chain integration tests (important)
  • fix ser/des standard tests
  • fix warnings

ccurme and others added 26 commits September 5, 2024 11:13
`_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).
@ccurme ccurme changed the base branch from main to v0.3rc September 8, 2024 15:24
@ccurme ccurme marked this pull request as ready for review September 9, 2024 19:35
@baskaryan baskaryan merged commit 2abd5f7 into v0.3rc Sep 11, 2024
18 checks passed
@baskaryan baskaryan deleted the cc/upgrade_pydantic branch September 11, 2024 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants