From 5fe2ff4a78f2c5663680643710cb6ba966a06e57 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Tue, 22 Jul 2025 18:56:46 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Speed=20up=20function=20`o?= =?UTF-8?q?penai=5Fmodel=5Fprofile`=20by=20241%=20REFINEMENT=20Here's=20a?= =?UTF-8?q?=20version=20of=20your=20program=20optimized=20for=20**runtime*?= =?UTF-8?q?*,=20based=20on=20the=20line=20profiling=20results=20and=20anal?= =?UTF-8?q?ysis.=20The=20*major=20slow=20point*=20in=20your=20program=20is?= =?UTF-8?q?=20the=20construction=20of=20the=20`OpenAIModelProfile`,=20whic?= =?UTF-8?q?h=20is=20being=20called=20thousands=20of=20times=20(`7158`=20hi?= =?UTF-8?q?ts=20in=20profiling).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Observations:** - If the constructor of `OpenAIModelProfile` and the arguments passed are pure (no side effects and depend only on arguments) and there are only a small number of possible configurations (i.e., `is_reasoning_model` is only `True` or `False`), you can cache the results. - The argument `model_name.startswith('o')` only leads to two possible outcomes for `openai_supports_sampling_settings`. - Everything else in the `OpenAIModelProfile` constructor arguments is constant. Thus, **memoizing/caching** the return value based on the boolean `is_reasoning_model` will save a lot of time. --- --- **Summary of optimizations:** - Moved the expensive constructor to an `@lru_cache`-decorated helper function, so the `OpenAIModelProfile` object is only created at most twice (one per each type of `is_reasoning_model`). - Future calls for the same type immediately return the cached object, making the function nearly instantaneous after the first call per type. - Preserved all original comments. This will **dramatically improve runtime** for workloads where this function is called repeatedly. If there are more than two options or you allow more variations in input, simply adjust the caching logic or the cache key. --- pydantic_ai_slim/pydantic_ai/profiles/openai.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pydantic_ai_slim/pydantic_ai/profiles/openai.py b/pydantic_ai_slim/pydantic_ai/profiles/openai.py index 4367216add..653945dc32 100644 --- a/pydantic_ai_slim/pydantic_ai/profiles/openai.py +++ b/pydantic_ai_slim/pydantic_ai/profiles/openai.py @@ -2,6 +2,7 @@ import re from dataclasses import dataclass +from functools import lru_cache from typing import Any from . import ModelProfile @@ -28,6 +29,10 @@ def openai_model_profile(model_name: str) -> ModelProfile: # Structured Outputs (output mode 'native') is only supported with the gpt-4o-mini, gpt-4o-mini-2024-07-18, and gpt-4o-2024-08-06 model snapshots and later. # We leave it in here for all models because the `default_structured_output_mode` is `'tool'`, so `native` is only used # when the user specifically uses the `NativeOutput` marker, so an error from the API is acceptable. + return _get_openai_model_profile(is_reasoning_model) + +@lru_cache(maxsize=2) +def _get_openai_model_profile(is_reasoning_model: bool) -> ModelProfile: return OpenAIModelProfile( json_schema_transformer=OpenAIJsonSchemaTransformer, supports_json_schema_output=True,