@@ -57,9 +57,7 @@ def create_request_copy(request: Request):
57
57
}
58
58
59
59
60
- def is_passthrough_request_using_router_model (
61
- request_body : dict , llm_router : Optional [litellm .Router ]
62
- ) -> bool :
60
+ def is_passthrough_request_using_router_model (request_body : dict , llm_router : Optional [litellm .Router ]) -> bool :
63
61
"""
64
62
Returns True if the model is in the llm_router model names
65
63
"""
@@ -95,16 +93,12 @@ async def llm_passthrough_factory_proxy_route(
95
93
model = None ,
96
94
)
97
95
if provider_config is None :
98
- raise HTTPException (
99
- status_code = 404 , detail = f"Provider { custom_llm_provider } not found"
100
- )
96
+ raise HTTPException (status_code = 404 , detail = f"Provider { custom_llm_provider } not found" )
101
97
102
98
base_target_url = provider_config .get_api_base ()
103
99
104
100
if base_target_url is None :
105
- raise HTTPException (
106
- status_code = 404 , detail = f"Provider { custom_llm_provider } api base not found"
107
- )
101
+ raise HTTPException (status_code = 404 , detail = f"Provider { custom_llm_provider } api base not found" )
108
102
109
103
encoded_endpoint = httpx .URL (endpoint ).path
110
104
@@ -183,17 +177,11 @@ async def gemini_proxy_route(
183
177
[Docs](https://docs.litellm.ai/docs/pass_through/google_ai_studio)
184
178
"""
185
179
## CHECK FOR LITELLM API KEY IN THE QUERY PARAMS - ?..key=LITELLM_API_KEY
186
- google_ai_studio_api_key = request .query_params .get ("key" ) or request .headers .get (
187
- "x-goog-api-key"
188
- )
180
+ google_ai_studio_api_key = request .query_params .get ("key" ) or request .headers .get ("x-goog-api-key" )
189
181
190
- user_api_key_dict = await user_api_key_auth (
191
- request = request , api_key = f"Bearer { google_ai_studio_api_key } "
192
- )
182
+ user_api_key_dict = await user_api_key_auth (request = request , api_key = f"Bearer { google_ai_studio_api_key } " )
193
183
194
- base_target_url = (
195
- os .getenv ("GEMINI_API_BASE" ) or "https://generativelanguage.googleapis.com"
196
- )
184
+ base_target_url = os .getenv ("GEMINI_API_BASE" ) or "https://generativelanguage.googleapis.com"
197
185
encoded_endpoint = httpx .URL (endpoint ).path
198
186
199
187
# Ensure endpoint starts with '/' for proper URL construction
@@ -226,6 +214,7 @@ async def gemini_proxy_route(
226
214
endpoint_func = create_pass_through_route (
227
215
endpoint = endpoint ,
228
216
target = str (updated_url ),
217
+ custom_llm_provider = "gemini" ,
229
218
) # dynamically construct pass-through endpoint based on incoming path
230
219
received_value = await endpoint_func (
231
220
request ,
@@ -310,9 +299,7 @@ async def vllm_proxy_route(
310
299
from litellm .proxy .proxy_server import llm_router
311
300
312
301
request_body = await get_request_body (request )
313
- is_router_model = is_passthrough_request_using_router_model (
314
- request_body , llm_router
315
- )
302
+ is_router_model = is_passthrough_request_using_router_model (request_body , llm_router )
316
303
is_streaming_request = is_passthrough_request_streaming (request_body )
317
304
if is_router_model and llm_router :
318
305
result = cast (
@@ -327,11 +314,7 @@ async def vllm_proxy_route(
327
314
content = None ,
328
315
data = None ,
329
316
files = None ,
330
- json = (
331
- request_body
332
- if request .headers .get ("content-type" ) == "application/json"
333
- else None
334
- ),
317
+ json = (request_body if request .headers .get ("content-type" ) == "application/json" else None ),
335
318
params = None ,
336
319
headers = None ,
337
320
cookies = None ,
@@ -509,9 +492,7 @@ async def handle_bedrock_count_tokens(
509
492
# Extract model from request body
510
493
model = request_body .get ("model" )
511
494
if not model :
512
- raise HTTPException (
513
- status_code = 400 , detail = {"error" : "Model is required in request body" }
514
- )
495
+ raise HTTPException (status_code = 400 , detail = {"error" : "Model is required in request body" })
515
496
516
497
# Get model parameters from router
517
498
litellm_params = {"user_api_key_dict" : user_api_key_dict }
@@ -550,9 +531,7 @@ async def handle_bedrock_count_tokens(
550
531
raise
551
532
except Exception as e :
552
533
verbose_proxy_logger .error (f"Error in handle_bedrock_count_tokens: { str (e )} " )
553
- raise HTTPException (
554
- status_code = 500 , detail = {"error" : f"CountTokens processing error: { str (e )} " }
555
- )
534
+ raise HTTPException (status_code = 500 , detail = {"error" : f"CountTokens processing error: { str (e )} " })
556
535
557
536
558
537
async def bedrock_llm_proxy_route (
@@ -604,8 +583,7 @@ async def bedrock_llm_proxy_route(
604
583
raise HTTPException (
605
584
status_code = 400 ,
606
585
detail = {
607
- "error" : "Model missing from endpoint. Expected format: /model/<Model>/<endpoint>. Got: "
608
- + endpoint ,
586
+ "error" : "Model missing from endpoint. Expected format: /model/<Model>/<endpoint>. Got: " + endpoint ,
609
587
},
610
588
)
611
589
@@ -669,9 +647,7 @@ async def bedrock_proxy_route(
669
647
670
648
aws_region_name = litellm .utils .get_secret (secret_name = "AWS_REGION_NAME" )
671
649
if _is_bedrock_agent_runtime_route (endpoint = endpoint ): # handle bedrock agents
672
- base_target_url = (
673
- f"https://bedrock-agent-runtime.{ aws_region_name } .amazonaws.com"
674
- )
650
+ base_target_url = f"https://bedrock-agent-runtime.{ aws_region_name } .amazonaws.com"
675
651
else :
676
652
return await bedrock_llm_proxy_route (
677
653
endpoint = endpoint ,
@@ -701,9 +677,7 @@ async def bedrock_proxy_route(
701
677
data = await request .json ()
702
678
except Exception as e :
703
679
raise HTTPException (status_code = 400 , detail = {"error" : e })
704
- _request = AWSRequest (
705
- method = "POST" , url = str (updated_url ), data = json .dumps (data ), headers = headers
706
- )
680
+ _request = AWSRequest (method = "POST" , url = str (updated_url ), data = json .dumps (data ), headers = headers )
707
681
sigv4 .add_auth (_request )
708
682
prepped = _request .prepare ()
709
683
@@ -764,14 +738,8 @@ async def assemblyai_proxy_route(
764
738
[Docs](https://api.assemblyai.com)
765
739
"""
766
740
# Set base URL based on the route
767
- assembly_region = AssemblyAIPassthroughLoggingHandler ._get_assembly_region_from_url (
768
- url = str (request .url )
769
- )
770
- base_target_url = (
771
- AssemblyAIPassthroughLoggingHandler ._get_assembly_base_url_from_region (
772
- region = assembly_region
773
- )
774
- )
741
+ assembly_region = AssemblyAIPassthroughLoggingHandler ._get_assembly_region_from_url (url = str (request .url ))
742
+ base_target_url = AssemblyAIPassthroughLoggingHandler ._get_assembly_base_url_from_region (region = assembly_region )
775
743
encoded_endpoint = httpx .URL (endpoint ).path
776
744
# Ensure endpoint starts with '/' for proper URL construction
777
745
if not encoded_endpoint .startswith ("/" ):
@@ -829,18 +797,14 @@ async def azure_proxy_route(
829
797
"""
830
798
base_target_url = get_secret_str (secret_name = "AZURE_API_BASE" )
831
799
if base_target_url is None :
832
- raise Exception (
833
- "Required 'AZURE_API_BASE' in environment to make pass-through calls to Azure."
834
- )
800
+ raise Exception ("Required 'AZURE_API_BASE' in environment to make pass-through calls to Azure." )
835
801
# Add or update query parameters
836
802
azure_api_key = passthrough_endpoint_router .get_credentials (
837
803
custom_llm_provider = litellm .LlmProviders .AZURE .value ,
838
804
region_name = None ,
839
805
)
840
806
if azure_api_key is None :
841
- raise Exception (
842
- "Required 'AZURE_API_KEY' in environment to make pass-through calls to Azure."
843
- )
807
+ raise Exception ("Required 'AZURE_API_KEY' in environment to make pass-through calls to Azure." )
844
808
845
809
return await BaseOpenAIPassThroughHandler ._base_openai_pass_through_handler (
846
810
endpoint = endpoint ,
@@ -864,9 +828,7 @@ def get_default_base_target_url(vertex_location: Optional[str]) -> str:
864
828
865
829
@staticmethod
866
830
@abstractmethod
867
- def update_base_target_url_with_credential_location (
868
- base_target_url : str , vertex_location : Optional [str ]
869
- ) -> str :
831
+ def update_base_target_url_with_credential_location (base_target_url : str , vertex_location : Optional [str ]) -> str :
870
832
pass
871
833
872
834
@@ -876,9 +838,7 @@ def get_default_base_target_url(vertex_location: Optional[str]) -> str:
876
838
return "https://discoveryengine.googleapis.com/"
877
839
878
840
@staticmethod
879
- def update_base_target_url_with_credential_location (
880
- base_target_url : str , vertex_location : Optional [str ]
881
- ) -> str :
841
+ def update_base_target_url_with_credential_location (base_target_url : str , vertex_location : Optional [str ]) -> str :
882
842
return base_target_url
883
843
884
844
@@ -888,9 +848,7 @@ def get_default_base_target_url(vertex_location: Optional[str]) -> str:
888
848
return get_vertex_base_url (vertex_location )
889
849
890
850
@staticmethod
891
- def update_base_target_url_with_credential_location (
892
- base_target_url : str , vertex_location : Optional [str ]
893
- ) -> str :
851
+ def update_base_target_url_with_credential_location (base_target_url : str , vertex_location : Optional [str ]) -> str :
894
852
return get_vertex_base_url (vertex_location )
895
853
896
854
@@ -956,18 +914,14 @@ async def _base_vertex_proxy_route(
956
914
location = vertex_location ,
957
915
)
958
916
959
- base_target_url = get_vertex_pass_through_handler .get_default_base_target_url (
960
- vertex_location
961
- )
917
+ base_target_url = get_vertex_pass_through_handler .get_default_base_target_url (vertex_location )
962
918
963
919
headers_passed_through = False
964
920
# Use headers from the incoming request if no vertex credentials are found
965
921
if vertex_credentials is None or vertex_credentials .vertex_project is None :
966
922
headers = dict (request .headers ) or {}
967
923
headers_passed_through = True
968
- verbose_proxy_logger .debug (
969
- "default_vertex_config not set, incoming request headers %s" , headers
970
- )
924
+ verbose_proxy_logger .debug ("default_vertex_config not set, incoming request headers %s" , headers )
971
925
headers .pop ("content-length" , None )
972
926
headers .pop ("host" , None )
973
927
else :
@@ -1133,9 +1087,7 @@ async def openai_proxy_route(
1133
1087
region_name = None ,
1134
1088
)
1135
1089
if openai_api_key is None :
1136
- raise Exception (
1137
- "Required 'OPENAI_API_KEY' in environment to make pass-through calls to OpenAI."
1138
- )
1090
+ raise Exception ("Required 'OPENAI_API_KEY' in environment to make pass-through calls to OpenAI." )
1139
1091
1140
1092
return await BaseOpenAIPassThroughHandler ._base_openai_pass_through_handler (
1141
1093
endpoint = endpoint ,
@@ -1181,9 +1133,7 @@ async def _base_openai_pass_through_handler(
1181
1133
endpoint_func = create_pass_through_route (
1182
1134
endpoint = endpoint ,
1183
1135
target = str (updated_url ),
1184
- custom_headers = BaseOpenAIPassThroughHandler ._assemble_headers (
1185
- api_key = api_key , request = request
1186
- ),
1136
+ custom_headers = BaseOpenAIPassThroughHandler ._assemble_headers (api_key = api_key , request = request ),
1187
1137
) # dynamically construct pass-through endpoint based on incoming path
1188
1138
received_value = await endpoint_func (
1189
1139
request ,
@@ -1200,10 +1150,7 @@ def _append_openai_beta_header(headers: dict, request: Request) -> dict:
1200
1150
"""
1201
1151
Appends the OpenAI-Beta header to the headers if the request is an OpenAI Assistants API request
1202
1152
"""
1203
- if (
1204
- RouteChecks ._is_assistants_api_request (request ) is True
1205
- and "OpenAI-Beta" not in headers
1206
- ):
1153
+ if RouteChecks ._is_assistants_api_request (request ) is True and "OpenAI-Beta" not in headers :
1207
1154
headers ["OpenAI-Beta" ] = "assistants=v2"
1208
1155
return headers
1209
1156
@@ -1219,9 +1166,7 @@ def _assemble_headers(api_key: str, request: Request) -> dict:
1219
1166
)
1220
1167
1221
1168
@staticmethod
1222
- def _join_url_paths (
1223
- base_url : httpx .URL , path : str , custom_llm_provider : litellm .LlmProviders
1224
- ) -> str :
1169
+ def _join_url_paths (base_url : httpx .URL , path : str , custom_llm_provider : litellm .LlmProviders ) -> str :
1225
1170
"""
1226
1171
Properly joins a base URL with a path, preserving any existing path in the base URL.
1227
1172
"""
@@ -1237,14 +1182,9 @@ def _join_url_paths(
1237
1182
joined_path_str = str (base_url .copy_with (path = full_path ))
1238
1183
1239
1184
# Apply OpenAI-specific path handling for both branches
1240
- if (
1241
- custom_llm_provider == litellm .LlmProviders .OPENAI
1242
- and "/v1/" not in joined_path_str
1243
- ):
1185
+ if custom_llm_provider == litellm .LlmProviders .OPENAI and "/v1/" not in joined_path_str :
1244
1186
# Insert v1 after api.openai.com for OpenAI requests
1245
- joined_path_str = joined_path_str .replace (
1246
- "api.openai.com/" , "api.openai.com/v1/"
1247
- )
1187
+ joined_path_str = joined_path_str .replace ("api.openai.com/" , "api.openai.com/v1/" )
1248
1188
1249
1189
return joined_path_str
1250
1190
0 commit comments