From 29a5d5a9af814f1efc7a5cf5a9e12e3694f0c641 Mon Sep 17 00:00:00 2001 From: g97iulio1609 Date: Sat, 28 Feb 2026 17:56:01 +0100 Subject: [PATCH] fix: bound TypeAdapter lru_cache to prevent memory leak in multi-threaded usage The unbounded lru_cache on TypeAdapter causes memory leak in multi-threaded environments. Generic types like ParsedResponseOutputMessage[MyClass] create new type objects in each thread, preventing cache hits and growing the cache without bound. Set maxsize=4096 to cap memory usage while still providing effective caching for typical workloads. Fixes #2672 --- src/openai/_models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openai/_models.py b/src/openai/_models.py index 810e49dfc5..4711ffd007 100644 --- a/src/openai/_models.py +++ b/src/openai/_models.py @@ -799,7 +799,7 @@ class GenericModel(BaseGenericModel, BaseModel): if not PYDANTIC_V1: from pydantic import TypeAdapter as _TypeAdapter - _CachedTypeAdapter = cast("TypeAdapter[object]", lru_cache(maxsize=None)(_TypeAdapter)) + _CachedTypeAdapter = cast("TypeAdapter[object]", lru_cache(maxsize=4096)(_TypeAdapter)) if TYPE_CHECKING: from pydantic import TypeAdapter