1414from pydantic_ai .providers .openai import OpenAIProvider
1515from pydantic_ai .providers .openrouter import OpenRouterProvider
1616
17+ from code_puppy .tools .common import console
18+
1719# Environment variables used in this module:
1820# - GEMINI_API_KEY: API key for Google's Gemini models. Required when using Gemini models.
1921# - OPENAI_API_KEY: API key for OpenAI models. Required when using OpenAI models or custom_openai endpoints.
@@ -46,6 +48,9 @@ def build_httpx_proxy(proxy):
4648 proxy_url = f"http://{ ip } :{ port } "
4749 proxy_auth = (username , password )
4850
51+ # Log the proxy being used
52+ console .log (f"Using proxy: { proxy_url } with username: { username } " )
53+
4954 return httpx .Proxy (url = proxy_url , auth = proxy_auth )
5055
5156
@@ -61,7 +66,11 @@ def get_random_proxy_from_file(file_path):
6166 raise ValueError (f"Proxy file '{ file_path } ' is empty or contains only whitespace." )
6267
6368 selected_proxy = random .choice (proxies )
64- return build_httpx_proxy (selected_proxy )
69+ try :
70+ return build_httpx_proxy (selected_proxy )
71+ except ValueError as e :
72+ console .log (f"Warning: Malformed proxy '{ selected_proxy } ' found in file '{ file_path } ', ignoring and continuing without proxy." )
73+ return None
6574
6675
6776def get_custom_config (model_config ):
@@ -145,7 +154,11 @@ def get_model(model_name: str, config: Dict[str, Any]) -> Any:
145154 if proxy_file_path :
146155 proxy = get_random_proxy_from_file (proxy_file_path )
147156
148- client = httpx .AsyncClient (headers = headers , verify = ca_certs_path , proxy = proxy )
157+ # Only pass proxy to client if it's valid
158+ client_args = {"headers" : headers , "verify" : ca_certs_path }
159+ if proxy is not None :
160+ client_args ["proxy" ] = proxy
161+ client = httpx .AsyncClient (** client_args )
149162 anthropic_client = AsyncAnthropic (
150163 base_url = url ,
151164 http_client = client ,
@@ -217,7 +230,11 @@ def get_model(model_name: str, config: Dict[str, Any]) -> Any:
217230 if proxy_file_path :
218231 proxy = get_random_proxy_from_file (proxy_file_path )
219232
220- client = httpx .AsyncClient (headers = headers , verify = ca_certs_path , proxy = proxy )
233+ # Only pass proxy to client if it's valid
234+ client_args = {"headers" : headers , "verify" : ca_certs_path }
235+ if proxy is not None :
236+ client_args ["proxy" ] = proxy
237+ client = httpx .AsyncClient (** client_args )
221238 provider_args = dict (
222239 base_url = url ,
223240 http_client = client ,
0 commit comments