Skip to content

Commit

Permalink
feat: adapt pydantic v2
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanFM committed Mar 7, 2024
1 parent c6f4903 commit 62f6c98
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
6 changes: 6 additions & 0 deletions jina/_docarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@
from docarray import Document, DocumentArray

docarray_v2 = False


import pydantic

is_pydantic_v2 = pydantic.__version__.startswith('2.')

7 changes: 5 additions & 2 deletions jina/serve/runtimes/gateway/graph/topology_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import grpc.aio

from jina._docarray import DocumentArray, docarray_v2
from jina._docarray import DocumentArray, docarray_v2, is_pydantic_v2
from jina.constants import __default_endpoint__
from jina.excepts import InternalNetworkError
from jina.logging.logger import JinaLogger
Expand All @@ -20,7 +20,10 @@
from docarray import DocList
from docarray.documents.legacy import LegacyDocument

from jina.serve.runtimes.helper import _create_pydantic_model_from_schema
if not is_pydantic_v2:
from jina.serve.runtimes.helper import _create_pydantic_model_from_schema
else:


legacy_doc_schema = LegacyDocument.schema()

Expand Down
4 changes: 2 additions & 2 deletions jina/serve/runtimes/helper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import copy
from typing import Any, Dict, List, Optional, Tuple, Union

from jina._docarray import docarray_v2
from jina._docarray import docarray_v2, is_pydantic_v2

_SPECIFIC_EXECUTOR_SEPARATOR = '__'

Expand Down Expand Up @@ -79,7 +79,7 @@ def _parse_specific_params(parameters: Dict, executor_name: str):
return parsed_params


if docarray_v2:
if docarray_v2 and not is_pydantic_v2:
from docarray import BaseDoc, DocList
from docarray.typing import AnyTensor
from pydantic import create_model
Expand Down
11 changes: 7 additions & 4 deletions jina/serve/runtimes/worker/request_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from google.protobuf.struct_pb2 import Struct

from jina._docarray import DocumentArray, docarray_v2
from jina._docarray import DocumentArray, docarray_v2, is_pydantic_v2
from jina.constants import __default_endpoint__
from jina.excepts import BadConfigSource, RuntimeTerminated
from jina.helper import get_full_version
Expand Down Expand Up @@ -1013,21 +1013,24 @@ async def endpoint_discovery(self, empty, context) -> jina_pb2.EndpointsProto:
if docarray_v2:
from docarray.documents.legacy import LegacyDocument

from jina.serve.runtimes.helper import _create_aux_model_doc_list_to_list
if not is_pydantic_v2:
from jina.serve.runtimes.helper import _create_aux_model_doc_list_to_list as create_pure_python_type_model
else:
from docarray.utils.create_dynamic_doc_class import create_pure_python_type_model

legacy_doc_schema = LegacyDocument.schema()
for endpoint_name, inner_dict in schemas.items():
if inner_dict['input']['model'].schema() == legacy_doc_schema:
inner_dict['input']['model'] = legacy_doc_schema
else:
inner_dict['input']['model'] = _create_aux_model_doc_list_to_list(
inner_dict['input']['model'] = create_pure_python_type_model(
inner_dict['input']['model']
).schema()

if inner_dict['output']['model'].schema() == legacy_doc_schema:
inner_dict['output']['model'] = legacy_doc_schema
else:
inner_dict['output']['model'] = _create_aux_model_doc_list_to_list(
inner_dict['output']['model'] = create_pure_python_type_model(
inner_dict['output']['model']
).schema()

Expand Down

0 comments on commit 62f6c98

Please sign in to comment.