Home / Function/ _convert_any_typed_dicts_to_pydantic() — langchain Function Reference

_convert_any_typed_dicts_to_pydantic() — langchain Function Reference

Architecture documentation for the _convert_any_typed_dicts_to_pydantic() function in function_calling.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  cac19dca_c263_1d95_44f8_9add2fd569fc["_convert_any_typed_dicts_to_pydantic()"]
  344b2838_87a8_d5dc_b550_fdb443ff6c4e["function_calling.py"]
  cac19dca_c263_1d95_44f8_9add2fd569fc -->|defined in| 344b2838_87a8_d5dc_b550_fdb443ff6c4e
  afe6c584_4f78_9f27_a452_c0af79a40037["_convert_typed_dict_to_openai_function()"]
  afe6c584_4f78_9f27_a452_c0af79a40037 -->|calls| cac19dca_c263_1d95_44f8_9add2fd569fc
  5f687ccf_c566_bcae_3531_656b07ba3bcd["_parse_google_docstring()"]
  cac19dca_c263_1d95_44f8_9add2fd569fc -->|calls| 5f687ccf_c566_bcae_3531_656b07ba3bcd
  de3a86d8_5b85_ad29_8606_392f509675ae["_py_38_safe_origin()"]
  cac19dca_c263_1d95_44f8_9add2fd569fc -->|calls| de3a86d8_5b85_ad29_8606_392f509675ae
  style cac19dca_c263_1d95_44f8_9add2fd569fc fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/utils/function_calling.py lines 244–311

def _convert_any_typed_dicts_to_pydantic(
    type_: type,
    *,
    visited: dict[type, type],
    depth: int = 0,
) -> type:
    if type_ in visited:
        return visited[type_]
    if depth >= _MAX_TYPED_DICT_RECURSION:
        return type_
    if is_typeddict(type_):
        typed_dict = type_
        docstring = inspect.getdoc(typed_dict)
        # Use get_type_hints to properly resolve forward references and
        # string annotations in Python 3.14+ (PEP 649 deferred annotations).
        # include_extras=True preserves Annotated metadata.
        try:
            annotations_ = get_type_hints(typed_dict, include_extras=True)
        except Exception:
            # Fallback for edge cases where get_type_hints might fail
            annotations_ = typed_dict.__annotations__
        description, arg_descriptions = _parse_google_docstring(
            docstring, list(annotations_)
        )
        fields: dict = {}
        for arg, arg_type in annotations_.items():
            if get_origin(arg_type) in {Annotated, typing_extensions.Annotated}:
                annotated_args = get_args(arg_type)
                new_arg_type = _convert_any_typed_dicts_to_pydantic(
                    annotated_args[0], depth=depth + 1, visited=visited
                )
                field_kwargs = dict(
                    zip(("default", "description"), annotated_args[1:], strict=False)
                )
                if (field_desc := field_kwargs.get("description")) and not isinstance(
                    field_desc, str
                ):
                    msg = (
                        f"Invalid annotation for field {arg}. Third argument to "
                        f"Annotated must be a string description, received value of "
                        f"type {type(field_desc)}."
                    )
                    raise ValueError(msg)
                if arg_desc := arg_descriptions.get(arg):
                    field_kwargs["description"] = arg_desc
                fields[arg] = (new_arg_type, Field_v1(**field_kwargs))
            else:
                new_arg_type = _convert_any_typed_dicts_to_pydantic(
                    arg_type, depth=depth + 1, visited=visited
                )
                field_kwargs = {"default": ...}
                if arg_desc := arg_descriptions.get(arg):
                    field_kwargs["description"] = arg_desc
                fields[arg] = (new_arg_type, Field_v1(**field_kwargs))
        model = cast(
            "type[BaseModelV1]", create_model_v1(typed_dict.__name__, **fields)
        )
        model.__doc__ = description
        visited[typed_dict] = model
        return model
    if (origin := get_origin(type_)) and (type_args := get_args(type_)):
        subscriptable_origin = _py_38_safe_origin(origin)
        type_args = tuple(
            _convert_any_typed_dicts_to_pydantic(arg, depth=depth + 1, visited=visited)
            for arg in type_args
        )
        return cast("type", subscriptable_origin[type_args])  # type: ignore[index]
    return type_

Subdomains

Frequently Asked Questions

What does _convert_any_typed_dicts_to_pydantic() do?
_convert_any_typed_dicts_to_pydantic() is a function in the langchain codebase, defined in libs/core/langchain_core/utils/function_calling.py.
Where is _convert_any_typed_dicts_to_pydantic() defined?
_convert_any_typed_dicts_to_pydantic() is defined in libs/core/langchain_core/utils/function_calling.py at line 244.
What does _convert_any_typed_dicts_to_pydantic() call?
_convert_any_typed_dicts_to_pydantic() calls 2 function(s): _parse_google_docstring, _py_38_safe_origin.
What calls _convert_any_typed_dicts_to_pydantic()?
_convert_any_typed_dicts_to_pydantic() is called by 1 function(s): _convert_typed_dict_to_openai_function.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free