perf(langchain): add lru cache when resolving types (#36649)

speed up for re-instantiation of the same agent
This commit is contained in:
Eugene Yurtsev
2026-04-10 11:25:58 -04:00
committed by GitHub
parent 1ca47a5411
commit 2c9296c423

View File

@@ -2,6 +2,7 @@
from __future__ import annotations
import functools
import itertools
from dataclasses import dataclass, field, fields
from typing import (
@@ -399,9 +400,15 @@ def _chain_async_model_call_handlers(
return composed_handler
@functools.lru_cache(maxsize=100)
def _get_schema_type_hints(schema: type) -> dict[str, Any]:
"""Return cached type hints for a schema."""
return get_type_hints(schema, include_extras=True)
def _resolve_schemas(schemas: set[type]) -> tuple[type, type, type]:
"""Resolve state, input, and output schemas for the given schemas."""
schema_hints = {schema: get_type_hints(schema, include_extras=True) for schema in schemas}
schema_hints = {schema: _get_schema_type_hints(schema) for schema in schemas}
return (
_resolve_schema(schema_hints, "StateSchema", None),
_resolve_schema(schema_hints, "InputSchema", "input"),