From c1b2dff59565d38a3399006e0505eae524dca349 Mon Sep 17 00:00:00 2001 From: Krish Dholakia Date: Sat, 8 Nov 2025 10:33:47 -0800 Subject: [PATCH] fix(pass_through_endpoints.py): improve clearing logic - only remove unvisited endpoints (#16400) simpler than clear all, and try to re-add --- .../pass_through_endpoints.py | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py b/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py index 669988d683..f210dbba07 100644 --- a/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py +++ b/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py @@ -1893,6 +1893,11 @@ class InitPassThroughEndpointHelpers: """Clear all pass-through routes from the registry""" _registered_pass_through_routes.clear() + @staticmethod + def get_registered_pass_through_endpoints_keys() -> List[str]: + """Get all registered pass-through endpoints from the registry""" + return list(_registered_pass_through_routes.keys()) + @staticmethod def is_registered_pass_through_route(route: str) -> bool: """ @@ -1995,7 +2000,16 @@ async def initialize_pass_through_endpoints( combined_pass_through_endpoints = pass_through_endpoints # type: ignore ## clear all existing pass-through endpoints from the FastAPI app routes - InitPassThroughEndpointHelpers.clear_all_pass_through_routes() + # InitPassThroughEndpointHelpers.clear_all_pass_through_routes() + + # get a list of all registered pass-through endpoints + # mark the ones that are visited in the list + # remove the ones that are not visited from the list + registered_pass_through_endpoints = ( + InitPassThroughEndpointHelpers.get_registered_pass_through_endpoints_keys() + ) + + visited_endpoints = set() for endpoint in combined_pass_through_endpoints: if isinstance(endpoint, PassThroughGenericEndpoint): @@ -2049,6 +2063,8 @@ async def initialize_pass_through_endpoints( endpoint_id=endpoint_id, ) + visited_endpoints.add(f"{endpoint_id}:exact:{_path}") + # Add wildcard route for sub-paths if endpoint.get("include_subpath", False) is True: InitPassThroughEndpointHelpers.add_subpath_route( @@ -2063,10 +2079,17 @@ async def initialize_pass_through_endpoints( endpoint_id=endpoint_id, ) + visited_endpoints.add(f"{endpoint_id}:subpath:{_path}") + verbose_proxy_logger.debug( "Added new pass through endpoint: %s (ID: %s)", _path, endpoint_id ) + # remove the ones that are not visited from the list + for endpoint in registered_pass_through_endpoints: + if endpoint not in visited_endpoints: + InitPassThroughEndpointHelpers.remove_endpoint_routes(endpoint) + async def _get_pass_through_endpoints_from_db( endpoint_id: Optional[str] = None,