From e102f181acffcdb181267203ed7fdce6df2ae360 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 30 Oct 2026 06:08:28 -0700 Subject: [PATCH] fix(responses): keep access_programs on chat completions on OpenAI SDK 3 The Responses-to-chat bridge hid every field the installed SDK's Response declared, so SDK 3 dropped access_programs, prompt_cache_options, and prompt_cache_diagnostics and let metadata through on streams. A fixed list gives the same output on SDK 2 and SDK 3. --- .github/workflows/test-unit.yml | 2 + .../transformation.py | 17 +++- ...responses_transformation_transformation.py | 92 +++++++++++++++++++ 3 files changed, 107 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test-unit.yml b/.github/workflows/test-unit.yml index 68823d99519..34c6cc2ae5e 100644 --- a/.github/workflows/test-unit.yml +++ b/.github/workflows/test-unit.yml @@ -1382,6 +1382,8 @@ jobs: tests/unit/types/test_router.py \ tests/unit/test_exception_header_preservation.py \ tests/unit/integrations/otel/test_otel_v2_sources_of_truth.py::test_speech_binary_response_still_streaming_reports_the_bytes_downloaded_so_far \ + tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py::test_non_stream_response_keeps_provider_metadata_and_drops_request_echoes \ + tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py::test_streaming_final_chunk_keeps_provider_metadata_and_drops_request_echoes \ --tb=short -q - name: Run workload identity tests with SDK 3 diff --git a/litellm/completion_extras/litellm_responses_transformation/transformation.py b/litellm/completion_extras/litellm_responses_transformation/transformation.py index bb59b4617af..f156508f09a 100644 --- a/litellm/completion_extras/litellm_responses_transformation/transformation.py +++ b/litellm/completion_extras/litellm_responses_transformation/transformation.py @@ -8,8 +8,6 @@ from itertools import accumulate, chain from types import MappingProxyType from typing import TYPE_CHECKING, Any, Final, Literal, TypedDict, TypeVar, Union, cast -from openai.types.chat import ChatCompletion -from openai.types.responses import Response from openai.types.responses.custom_tool_param import CustomToolParam from openai.types.responses.response_input_param import ( FunctionCallOutput, @@ -92,8 +90,19 @@ if TYPE_CHECKING: _CHAT_COMPLETION_FIELDS: Final = frozenset((*ModelResponse.model_fields, "usage")) -_RESPONSES_API_ONLY_FIELDS: Final = frozenset((*Response.model_fields, *ResponsesAPIResponse.model_fields)) - frozenset( - ChatCompletion.model_fields +_RESPONSES_API_ONLY_FIELDS: Final = frozenset( + ( + *ResponsesAPIResponse.model_fields, + "background", + "completed_at", + "conversation", + "max_tool_calls", + "prompt", + "prompt_cache_key", + "prompt_cache_retention", + "safety_identifier", + "top_logprobs", + ) ) diff --git a/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py b/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py index 4e6ed16e360..dc4e7f38a94 100644 --- a/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py +++ b/tests/unit/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py @@ -4992,6 +4992,98 @@ def test_streaming_final_chunk_carries_provider_metadata(): assert all("service_tier" not in chunk for chunk in chunks[:-1]) +_RESPONSES_BODY_METADATA: Final = { + "access_programs": {"cyber": "daybreak_blue"}, + "prompt_cache_options": {"mode": "implicit", "ttl": "30m"}, + "prompt_cache_diagnostics": {"type": "cache_hit"}, +} +_RESPONSES_REQUEST_ECHOES: Final = { + "metadata": {"trace": "abc"}, + "background": False, + "completed_at": 1760144905, + "conversation": {"id": "conv_metadata"}, + "max_tool_calls": 3, + "prompt": {"id": "pmpt_metadata"}, + "prompt_cache_key": "cache-key", + "prompt_cache_retention": "24h", + "safety_identifier": "user-hash", + "top_logprobs": 0, +} + + +def test_non_stream_response_keeps_provider_metadata_and_drops_request_echoes() -> None: + from openai.types.responses import ResponseOutputMessage, ResponseOutputText + + from litellm.types.llms.openai import ResponsesAPIResponse + from litellm.types.utils import ModelResponse + + handler: Final = LiteLLMResponsesTransformationHandler() + output_message: Final = ResponseOutputMessage( + id="msg_metadata", + content=[ResponseOutputText(annotations=[], text="hi", type="output_text", logprobs=[])], + role="assistant", + status="completed", + type="message", + ) + raw_response: Final = ResponsesAPIResponse( + id="resp_metadata", + created_at=1760144904, + model="gpt-5.6-sol", + object="response", + output=[output_message], + status="completed", + parallel_tool_calls=True, + tool_choice="auto", + tools=[], + **_RESPONSES_BODY_METADATA, + **_RESPONSES_REQUEST_ECHOES, + ) + + result: Final = handler.transform_response( + model="gpt-5.6-sol", + raw_response=raw_response, + model_response=ModelResponse(), + logging_obj=Mock(), + request_data={"model": "gpt-5.6-sol"}, + messages=[{"role": "user", "content": "hi"}], + optional_params={}, + litellm_params={"acompletion": False, "api_key": None}, + encoding=Mock(), + api_key=None, + json_mode=None, + ) + + dumped: Final = result.model_dump() + assert {key: dumped.get(key) for key in _RESPONSES_BODY_METADATA} == _RESPONSES_BODY_METADATA + assert not set(_RESPONSES_REQUEST_ECHOES) & set(dumped) + + +def test_streaming_final_chunk_keeps_provider_metadata_and_drops_request_echoes() -> None: + from litellm.completion_extras.litellm_responses_transformation.transformation import ( + OpenAiResponsesToChatCompletionStreamIterator, + ) + + iterator: Final = OpenAiResponsesToChatCompletionStreamIterator(streaming_response=None, sync_stream=True) + iterator.chunk_parser({"type": "response.created", "response": {"id": "resp_metadata", "output": []}}) + + final_chunk: Final = iterator.chunk_parser( + { + "type": "response.completed", + "response": { + "id": "resp_metadata", + "output": [{"type": "message"}], + "usage": {"input_tokens": 3, "output_tokens": 1, "total_tokens": 4}, + **_RESPONSES_BODY_METADATA, + **_RESPONSES_REQUEST_ECHOES, + }, + } + ) + + provider_fields: Final = final_chunk.provider_specific_fields or {} + assert {key: provider_fields.get(key) for key in _RESPONSES_BODY_METADATA} == _RESPONSES_BODY_METADATA + assert not set(_RESPONSES_REQUEST_ECHOES) & set(provider_fields) + + def _system_input_item(text: str) -> dict[str, object]: return {"type": "message", "role": "system", "content": [{"type": "input_text", "text": text}]}