From dcb974dd1eb00b2f214984265fddd8b3d44589d3 Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Tue, 23 Jul 2024 11:30:52 -0700 Subject: [PATCH] feat(utils.py): support passing openai response headers to client, if enabled Allows openai/openai-compatible provider response headers to be sent to client, if 'return_response_headers' is enabled --- litellm/proxy/_new_secret_config.yaml | 1 + litellm/tests/test_completion_cost.py | 3 +++ litellm/utils.py | 8 ++++++++ 3 files changed, 12 insertions(+) diff --git a/litellm/proxy/_new_secret_config.yaml b/litellm/proxy/_new_secret_config.yaml index 16570cbe1..a1af38379 100644 --- a/litellm/proxy/_new_secret_config.yaml +++ b/litellm/proxy/_new_secret_config.yaml @@ -7,3 +7,4 @@ model_list: litellm_settings: callbacks: ["logfire"] redact_user_api_key_info: true + return_response_headers: true diff --git a/litellm/tests/test_completion_cost.py b/litellm/tests/test_completion_cost.py index 22e82b29f..6e4425fb6 100644 --- a/litellm/tests/test_completion_cost.py +++ b/litellm/tests/test_completion_cost.py @@ -896,6 +896,9 @@ async def test_completion_cost_hidden_params(sync_mode): assert "response_cost" in response._hidden_params assert isinstance(response._hidden_params["response_cost"], float) + assert isinstance( + response._hidden_params["llm_provider-x-ratelimit-remaining-requests"], float + ) def test_vertex_ai_gemini_predict_cost(): diff --git a/litellm/utils.py b/litellm/utils.py index 97eb874d6..0beb041e9 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -5678,6 +5678,14 @@ def convert_to_model_response_object( _response_headers: Optional[dict] = None, ): received_args = locals() + if _response_headers is not None: + if hidden_params is not None: + hidden_params["additional_headers"] = { + "{}-{}".format("llm_provider", k): v + for k, v in _response_headers.items() + } + else: + hidden_params = {"additional_headers": _response_headers} ### CHECK IF ERROR IN RESPONSE ### - openrouter returns these in the dictionary if ( response_object is not None