From a7529aafc1d4f410083bde645beb040528ebba24 Mon Sep 17 00:00:00 2001 From: Joan Martinez Date: Fri, 16 Feb 2024 21:17:45 +0100 Subject: [PATCH 1/2] fix: fix issue 6140 --- jina/serve/runtimes/helper.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/jina/serve/runtimes/helper.py b/jina/serve/runtimes/helper.py index 70bb75a485c1b..ea6e4e8eb522b 100644 --- a/jina/serve/runtimes/helper.py +++ b/jina/serve/runtimes/helper.py @@ -99,7 +99,9 @@ def _parse_specific_params(parameters: Dict, executor_name: str): 'default', ] + def _create_aux_model_doc_list_to_list(model): + cached_models = set() fields: Dict[str, Any] = {} for field_name, field in model.__annotations__.items(): if field_name not in model.__fields__: @@ -108,8 +110,12 @@ def _create_aux_model_doc_list_to_list(model): try: if issubclass(field, DocList): t: Any = field.doc_type - t_aux = _create_aux_model_doc_list_to_list(t) - fields[field_name] = (List[t_aux], field_info) + if t.__name__ in cached_models: + fields[field_name] = (List[t], field_info) + else: + t_aux = _create_aux_model_doc_list_to_list(t) + fields[field_name] = (List[t_aux], field_info) + cached_models.add(t.__name__) else: fields[field_name] = (field, field_info) except TypeError: @@ -121,6 +127,7 @@ def _create_aux_model_doc_list_to_list(model): **fields, ) + def _get_field_from_type( field_schema, field_name, @@ -264,6 +271,7 @@ def _get_field_from_type( ) return ret + def _create_pydantic_model_from_schema( schema: Dict[str, any], model_name: str, From 30b37f2041e567c7575a43ee984fb3e89f174f30 Mon Sep 17 00:00:00 2001 From: Joan Martinez Date: Mon, 19 Feb 2024 09:17:24 +0100 Subject: [PATCH 2/2] fix: use cached models from all endpoints --- jina/serve/runtimes/helper.py | 12 +++++++----- jina/serve/runtimes/worker/request_handling.py | 9 +++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/jina/serve/runtimes/helper.py b/jina/serve/runtimes/helper.py index ea6e4e8eb522b..5d3561bc6c397 100644 --- a/jina/serve/runtimes/helper.py +++ b/jina/serve/runtimes/helper.py @@ -100,8 +100,8 @@ def _parse_specific_params(parameters: Dict, executor_name: str): ] - def _create_aux_model_doc_list_to_list(model): - cached_models = set() + def _create_aux_model_doc_list_to_list(model, cached_models = None): + cached_models = cached_models or set() fields: Dict[str, Any] = {} for field_name, field in model.__annotations__.items(): if field_name not in model.__fields__: @@ -120,12 +120,14 @@ def _create_aux_model_doc_list_to_list(model): fields[field_name] = (field, field_info) except TypeError: fields[field_name] = (field, field_info) - return create_model( + new_model = create_model( model.__name__, __base__=model, __validators__=model.__validators__, - **fields, - ) + **fields) + cached_models.add(new_model.__name__) + + return new_model def _get_field_from_type( diff --git a/jina/serve/runtimes/worker/request_handling.py b/jina/serve/runtimes/worker/request_handling.py index 0849aaebb388d..5e8b7109adf5e 100644 --- a/jina/serve/runtimes/worker/request_handling.py +++ b/jina/serve/runtimes/worker/request_handling.py @@ -178,6 +178,7 @@ def call_handle(request): ] return self.process_single_data(request, None, is_generator=is_generator) + app = get_fastapi_app( request_models_map=request_models_map, caller=call_handle, **kwargs ) @@ -1001,6 +1002,7 @@ async def endpoint_discovery(self, empty, context) -> jina_pb2.EndpointsProto: endpoints_proto.write_endpoints.extend(list(self._executor.write_endpoints)) schemas = self._executor._get_endpoint_models_dict() if docarray_v2: + cached_aux_models = set() from docarray.documents.legacy import LegacyDocument from jina.serve.runtimes.helper import _create_aux_model_doc_list_to_list @@ -1011,16 +1013,14 @@ async def endpoint_discovery(self, empty, context) -> jina_pb2.EndpointsProto: inner_dict['input']['model'] = legacy_doc_schema else: inner_dict['input']['model'] = _create_aux_model_doc_list_to_list( - inner_dict['input']['model'] + inner_dict['input']['model'], cached_aux_models ).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'] + inner_dict['output']['model'], cached_aux_models ).schema() - if inner_dict['parameters']['model'] is not None: inner_dict['parameters']['model'] = inner_dict['parameters'][ 'model' @@ -1031,6 +1031,7 @@ async def endpoint_discovery(self, empty, context) -> jina_pb2.EndpointsProto: inner_dict['output']['model'] = inner_dict['output']['model'].schema() inner_dict['parameters'] = {} json_format.ParseDict(schemas, endpoints_proto.schemas) + self.logger.debug('return an endpoint discovery request') return endpoints_proto def _extract_tracing_context(