From b6dea9c5fc0cc08dac12835a48e8b0f31991db21 Mon Sep 17 00:00:00 2001 From: Denis Shulyaka Date: Mon, 7 Sep 2026 09:32:22 +0300 Subject: [PATCH] Fix OpenAI prompt caching (#181472) --- homeassistant/components/openai_conversation/entity.py | 1 + tests/components/openai_conversation/test_ai_task.py | 4 ++++ tests/components/openai_conversation/test_conversation.py | 5 +++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/openai_conversation/entity.py b/homeassistant/components/openai_conversation/entity.py index aa0d05ae98e9..8402b3d12058 100644 --- a/homeassistant/components/openai_conversation/entity.py +++ b/homeassistant/components/openai_conversation/entity.py @@ -517,6 +517,7 @@ class OpenAIBaseLLMEntity(Entity): input=messages, max_output_tokens=options.get(CONF_MAX_TOKENS, RECOMMENDED_MAX_TOKENS), user=chat_log.conversation_id, + prompt_cache_key=self.subentry.subentry_id, service_tier=options.get(CONF_SERVICE_TIER, RECOMMENDED_SERVICE_TIER), store=options.get(CONF_STORE_RESPONSES, RECOMMENDED_STORE_RESPONSES), stream=True, diff --git a/tests/components/openai_conversation/test_ai_task.py b/tests/components/openai_conversation/test_ai_task.py index dc2d70265766..b123dc497d4a 100644 --- a/tests/components/openai_conversation/test_ai_task.py +++ b/tests/components/openai_conversation/test_ai_task.py @@ -66,6 +66,10 @@ async def test_generate_data( assert result.data == "The test data" assert mock_create_stream.call_args is not None assert mock_create_stream.call_args.kwargs["store"] is expected_store + assert ( + mock_create_stream.call_args.kwargs["prompt_cache_key"] + == ai_task_entry.subentry_id + ) @pytest.mark.usefixtures("mock_init_component") diff --git a/tests/components/openai_conversation/test_conversation.py b/tests/components/openai_conversation/test_conversation.py index 933e57bc7d1b..199ccbb23b00 100644 --- a/tests/components/openai_conversation/test_conversation.py +++ b/tests/components/openai_conversation/test_conversation.py @@ -823,13 +823,13 @@ async def test_flex_tier_retry( @pytest.mark.parametrize( "subentry_options", [{CONF_CHAT_MODEL: "gpt-5.6-sol", CONF_PRO_MODE: True}] ) +@pytest.mark.usefixtures("mock_init_component") async def test_model_args( hass: HomeAssistant, mock_config_entry: MockConfigEntry, - mock_init_component, mock_create_stream: AsyncMock, snapshot: SnapshotAssertion, - subentry_options: dict, + subentry_options: dict[str, str | bool], ) -> None: """Test model arguments for various configuration.""" @@ -860,4 +860,5 @@ async def test_model_args( model_args = mock_create_stream.call_args.kwargs.copy() model_args.pop("input") assert model_args.pop("user") == result.conversation_id + assert model_args.pop("prompt_cache_key") == subentry.subentry_id assert model_args == snapshot