From 0db4830bd7cebefd62db429d36479fe273f01611 Mon Sep 17 00:00:00 2001 From: Alexander Song Date: Tue, 21 Jan 2025 13:27:59 -0800 Subject: [PATCH] add typeddict suppoer --- .../openinference/instrumentation/config.py | 9 +++++ .../tests/test_manual_instrumentation.py | 33 ++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/python/openinference-instrumentation/src/openinference/instrumentation/config.py b/python/openinference-instrumentation/src/openinference/instrumentation/config.py index 9100782b1..6856e8d26 100644 --- a/python/openinference-instrumentation/src/openinference/instrumentation/config.py +++ b/python/openinference-instrumentation/src/openinference/instrumentation/config.py @@ -23,6 +23,7 @@ Type, TypeVar, Union, + _TypedDictMeta, cast, get_args, get_origin, @@ -1176,6 +1177,14 @@ def _get_jsonschema_type(annotation_type: type) -> Dict[str, Any]: _, value_type = annotation_type_args result["additionalProperties"] = _get_jsonschema_type(value_type) return result + is_typed_dict_type = isinstance(annotation_type, _TypedDictMeta) + if is_typed_dict_type: + result = {"type": "object"} + properties = {} + for field_name, field_type in annotation_type.__annotations__.items(): + properties[field_name] = _get_jsonschema_type(field_type) + result["properties"] = properties + return result if ( pydantic is not None and isinstance(annotation_type, type) diff --git a/python/openinference-instrumentation/tests/test_manual_instrumentation.py b/python/openinference-instrumentation/tests/test_manual_instrumentation.py index 95dc6c38b..51d4a6a13 100644 --- a/python/openinference-instrumentation/tests/test_manual_instrumentation.py +++ b/python/openinference-instrumentation/tests/test_manual_instrumentation.py @@ -1,7 +1,7 @@ import json from dataclasses import dataclass from datetime import datetime -from typing import Any, Dict, List, Literal, Mapping, Optional, Sequence, Tuple, Union +from typing import Any, Dict, List, Literal, Mapping, Optional, Sequence, Tuple, TypedDict, Union import jsonschema import pydantic @@ -1300,6 +1300,17 @@ class PydanticModel(pydantic.BaseModel): bool_param: bool any_param: Any + class TypedDictModel(TypedDict): + """ + typed-dict-description + """ + + string_param: str + int_param: int + float_param: float + bool_param: bool + any_param: Any + AnnotatedWithTypeAlias: TypeAlias = Annotated[str, "This is a description"] def example_function( # type: ignore[no-untyped-def] @@ -1333,6 +1344,7 @@ def example_function( # type: ignore[no-untyped-def] annotated_string_param: Annotated[str, "This is a description"], annotated_param_with_type_alias: AnnotatedWithTypeAlias, pydantic_model_param: PydanticModel, + typed_dict_param: TypedDictModel, string_param_with_default: str = "default", untyped_param_with_default="default", ) -> None: @@ -1560,6 +1572,24 @@ def example_function( # type: ignore[no-untyped-def] "title": "PydanticModel", "type": "object", }, + "typed_dict_param": { + "type": "object", + "properties": { + "string_param": { + "type": "string", + }, + "int_param": { + "type": "integer", + }, + "float_param": { + "type": "number", + }, + "bool_param": { + "type": "boolean", + }, + "any_param": {}, + }, + }, "string_param_with_default": { "default": "default", "type": "string", @@ -1599,6 +1629,7 @@ def example_function( # type: ignore[no-untyped-def] "annotated_string_param", "annotated_param_with_type_alias", "pydantic_model_param", + "typed_dict_param", ], } jsonschema.Draft7Validator.check_schema(