Skip to content

Commit eaedf8a

Browse files
authored
Add prompt caching options to OpenAIChatModelSettings (#3678)
1 parent 50b53a7 commit eaedf8a

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

pydantic_ai_slim/pydantic_ai/models/openai.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,18 @@ class OpenAIChatModelSettings(ModelSettings, total=False):
231231
This feature is currently only supported for some OpenAI models.
232232
"""
233233

234+
openai_prompt_cache_key: str
235+
"""Used by OpenAI to cache responses for similar requests to optimize your cache hit rates.
236+
237+
See the [OpenAI Prompt Caching documentation](https://platform.openai.com/docs/guides/prompt-caching#how-it-works) for more information.
238+
"""
239+
240+
openai_prompt_cache_retention: Literal['in-memory', '24h']
241+
"""The retention policy for the prompt cache. Set to 24h to enable extended prompt caching, which keeps cached prefixes active for longer, up to a maximum of 24 hours.
242+
243+
See the [OpenAI Prompt Caching documentation](https://platform.openai.com/docs/guides/prompt-caching#how-it-works) for more information.
244+
"""
245+
234246

235247
@deprecated('Use `OpenAIChatModelSettings` instead.')
236248
class OpenAIModelSettings(OpenAIChatModelSettings, total=False):
@@ -580,6 +592,8 @@ async def _completions_create(
580592
logit_bias=model_settings.get('logit_bias', OMIT),
581593
logprobs=model_settings.get('openai_logprobs', OMIT),
582594
top_logprobs=model_settings.get('openai_top_logprobs', OMIT),
595+
prompt_cache_key=model_settings.get('openai_prompt_cache_key', OMIT),
596+
prompt_cache_retention=model_settings.get('openai_prompt_cache_retention', OMIT),
583597
extra_headers=extra_headers,
584598
extra_body=model_settings.get('extra_body'),
585599
)
@@ -1423,6 +1437,8 @@ async def _responses_create( # noqa: C901
14231437
user=model_settings.get('openai_user', OMIT),
14241438
text=text or OMIT,
14251439
include=include or OMIT,
1440+
prompt_cache_key=model_settings.get('openai_prompt_cache_key', OMIT),
1441+
prompt_cache_retention=model_settings.get('openai_prompt_cache_retention', OMIT),
14261442
extra_headers=extra_headers,
14271443
extra_body=model_settings.get('extra_body'),
14281444
)

0 commit comments

Comments
 (0)