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

Update pydantic import to fix b/343572011 in g3 #369

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/test_pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ jobs:
python --version
pip install .[dev]
python -m unittest
test_pydantic1:
name: Test Pydantic1
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Run tests
run: |
python --version
pip install .[dev]
pip install -U "pydantic<2"
python -m unittest

test3_10:
name: Test Py3.10
runs-on: ubuntu-latest
Expand Down
30 changes: 23 additions & 7 deletions google/generativeai/types/content_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,29 @@
# See the License for the specific language governing permissions and
# limitations under the License.


from __future__ import annotations

from collections.abc import Iterable, Mapping, Sequence

import io
import inspect
import mimetypes
import typing
from typing import Any, Callable, Union
from typing_extensions import TypedDict

import pydantic

from google.generativeai.types import file_types
from google.generativeai import protos

import pydantic

try:
pydantic.Field
except AttributeError:
from pydantic import v1 as pydantic

pydantic_major_version = int(pydantic.version.VERSION.split('.')[0])

if typing.TYPE_CHECKING:
import PIL.Image
import IPython.display
Expand Down Expand Up @@ -336,10 +343,19 @@ def _schema_for_function(
# param.default if param.default != inspect.Parameter.empty
# else None
# ),
field = pydantic.Field(
# We support user-provided descriptions.
description=descriptions.get(name, None)
)
if pydantic_major_version <= 1:
field = pydantic.Field(
# We do not support default values for now.
default=(
param.default if param.default != inspect.Parameter.empty
# ! Need to use Undefined instead of None
else pydantic.fields.Undefined
),
# We support user-provided descriptions.
description=descriptions.get(name, None),
)
else:
field = pydantic.Field(description=descriptions.get(name, None))

# 1. We infer the argument type here: use Any rather than None so
# it will not try to auto-infer the type based on the default value.
Expand Down
Loading