Spaces:
Paused
Paused
Disable HTTP/2 in httpx client for OpenAI and DeepSeek to avoid 421 Misdirected Request errors and ensure consistent connections in HF Spaces
Browse files82 unmodified lines838485868788888989909192919394959697989910010110210382 unmodified lines try: import httpx print(f" httpx version: {httpx.__version__}") # Test with default settings with httpx.Client(timeout=10) as client: resp = client.get("https://api.anthropic.com") print(f" ✅ httpx -> Anthropic HTTP {resp.status_code}") with httpx.Client(timeout=10) as client: print(f" ✅ httpx (default) -> Anthropic HTTP {resp.status_code}") # Test OpenAI with HTTP/2 disabled (avoids 421 Misdirected Request) with httpx.Client(timeout=10, http2=False) as client: resp = client.get("https://api.openai.com") print(f" ✅ httpx -> OpenAI HTTP {resp.status_code}") print(f" ✅ httpx (http1.1) -> OpenAI HTTP {resp.status_code}") # Also test with default HTTP/2 to compare try: with httpx.Client(timeout=10) as client: resp = client.get("https://api.openai.com") print(f" ✅ httpx (default) -> OpenAI HTTP {resp.status_code}") except Exception as e2: print(f" ⚠️ httpx (default/http2) -> OpenAI FAILED: {type(e2).__name__}: {e2}") except Exception as e: print(f" ❌ httpx -> {type(e).__name__}: {e}") 278 unmodified lines279280281282283282283284285286287287288288289290291292293390 unmodified lines68468568668568768868969069169269369469569669785 unmodified lines783784785782786787788789790791792793794795278 unmodified lines if provider == ModelProvider.OPENAI: if not OPENAI_API_KEY: raise ValueError(f"OpenAI API key not configured. Please set OPENAI_API_KEY environment variable to use {provider.value} provider.") # Use default httpx settings for better compatibility with async environments self.client = openai.OpenAI(api_key=OPENAI_API_KEY, timeout=120.0) # Disable HTTP/2 to avoid 421 Misdirected Request on HF Spaces self._http_client = httpx.Client(timeout=httpx.Timeout(120.0, connect=30.0), http2=False) self.client = openai.OpenAI(api_key=OPENAI_API_KEY, http_client=self._http_client) elif provider == ModelProvider.DEEPSEEK: if not DEEPSEEK_API_KEY: raise ValueError(f"DeepSeek API key not configured. Please set DEEPSEEK_API_KEY environment variable to use {provider.value} provider.") # Use default httpx settings for better compatibility with async environments self.client = openai.OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com", timeout=120.0) # Disable HTTP/2 for consistent connections self._http_client = httpx.Client(timeout=httpx.Timeout(120.0, connect=30.0), http2=False) self.client = openai.OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com", http_client=self._http_client) elif provider == ModelProvider.ANTHROPIC: if not ANTHROPIC_API_KEY: raise ValueError(f"Anthropic API key not configured. Please set ANTHROPIC_API_KEY environment variable to use {provider.value} provider.")390 unmodified lines raise Exception(f"Текст судового рішення занадто короткий або відсутній (довжина: {len(court_decision_text) if court_decision_text else 0} символів). Будь ласка, перевірте вхідні дані.")
if provider == ModelProvider.OPENAI.value: # Use context-managed httpx client to avoid socket/fd leaks in threaded Gradio env # Use custom httpx client with HTTP/2 disabled to avoid 421 Misdirected Request # errors on HF Spaces (Cloudflare HTTP/2 connection coalescing issue) http_client = None response_text = None try: http_client = httpx.Client( timeout=httpx.Timeout(120.0, connect=30.0), http2=False, ) client = OpenAI( api_key=OPENAI_API_KEY, 85 unmodified lines pass
if provider == ModelProvider.DEEPSEEK.value: # Use context-managed httpx client for DeepSeek to avoid socket/fd leaks # Use custom httpx client with HTTP/2 disabled for consistent connections http_client = None response_text = None try: http_client = httpx.Client( timeout=httpx.Timeout(120.0, connect=30.0), http2=False, ) client = OpenAI( api_key=DEEPSEEK_API_KEY, app.py+12-3
main.py+11-6