Skip to content

Commit 62f6c98

Browse files
committed
feat: adapt pydantic v2
1 parent c6f4903 commit 62f6c98

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

jina/_docarray.py

+6
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@
88
from docarray import Document, DocumentArray
99

1010
docarray_v2 = False
11+
12+
13+
import pydantic
14+
15+
is_pydantic_v2 = pydantic.__version__.startswith('2.')
16+

jina/serve/runtimes/gateway/graph/topology_graph.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import grpc.aio
99

10-
from jina._docarray import DocumentArray, docarray_v2
10+
from jina._docarray import DocumentArray, docarray_v2, is_pydantic_v2
1111
from jina.constants import __default_endpoint__
1212
from jina.excepts import InternalNetworkError
1313
from jina.logging.logger import JinaLogger
@@ -20,7 +20,10 @@
2020
from docarray import DocList
2121
from docarray.documents.legacy import LegacyDocument
2222

23-
from jina.serve.runtimes.helper import _create_pydantic_model_from_schema
23+
if not is_pydantic_v2:
24+
from jina.serve.runtimes.helper import _create_pydantic_model_from_schema
25+
else:
26+
2427

2528
legacy_doc_schema = LegacyDocument.schema()
2629

jina/serve/runtimes/helper.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import copy
22
from typing import Any, Dict, List, Optional, Tuple, Union
33

4-
from jina._docarray import docarray_v2
4+
from jina._docarray import docarray_v2, is_pydantic_v2
55

66
_SPECIFIC_EXECUTOR_SEPARATOR = '__'
77

@@ -79,7 +79,7 @@ def _parse_specific_params(parameters: Dict, executor_name: str):
7979
return parsed_params
8080

8181

82-
if docarray_v2:
82+
if docarray_v2 and not is_pydantic_v2:
8383
from docarray import BaseDoc, DocList
8484
from docarray.typing import AnyTensor
8585
from pydantic import create_model

jina/serve/runtimes/worker/request_handling.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from google.protobuf.struct_pb2 import Struct
2222

23-
from jina._docarray import DocumentArray, docarray_v2
23+
from jina._docarray import DocumentArray, docarray_v2, is_pydantic_v2
2424
from jina.constants import __default_endpoint__
2525
from jina.excepts import BadConfigSource, RuntimeTerminated
2626
from jina.helper import get_full_version
@@ -1013,21 +1013,24 @@ async def endpoint_discovery(self, empty, context) -> jina_pb2.EndpointsProto:
10131013
if docarray_v2:
10141014
from docarray.documents.legacy import LegacyDocument
10151015

1016-
from jina.serve.runtimes.helper import _create_aux_model_doc_list_to_list
1016+
if not is_pydantic_v2:
1017+
from jina.serve.runtimes.helper import _create_aux_model_doc_list_to_list as create_pure_python_type_model
1018+
else:
1019+
from docarray.utils.create_dynamic_doc_class import create_pure_python_type_model
10171020

10181021
legacy_doc_schema = LegacyDocument.schema()
10191022
for endpoint_name, inner_dict in schemas.items():
10201023
if inner_dict['input']['model'].schema() == legacy_doc_schema:
10211024
inner_dict['input']['model'] = legacy_doc_schema
10221025
else:
1023-
inner_dict['input']['model'] = _create_aux_model_doc_list_to_list(
1026+
inner_dict['input']['model'] = create_pure_python_type_model(
10241027
inner_dict['input']['model']
10251028
).schema()
10261029

10271030
if inner_dict['output']['model'].schema() == legacy_doc_schema:
10281031
inner_dict['output']['model'] = legacy_doc_schema
10291032
else:
1030-
inner_dict['output']['model'] = _create_aux_model_doc_list_to_list(
1033+
inner_dict['output']['model'] = create_pure_python_type_model(
10311034
inner_dict['output']['model']
10321035
).schema()
10331036

0 commit comments

Comments
 (0)