Spaces:
Trkaga
/
Runtime error

akhaliq HF Staff commited on
Commit
c77d732
·
1 Parent(s): f26c666

Fix OpenRouter streaming: Use OpenAI client with proper headers

Browse files

Issue: Streaming not working with OpenRouter models
Root cause: Using InferenceClient instead of OpenAI client for OpenRouter

Fix:
- Use OpenAI client directly for OpenRouter (as per their docs)
- Add required headers: HTTP-Referer and X-Title for OpenRouter ranking
- Remove artificial 0.01s delay - use asyncio.sleep(0) for immediate flush
- Add detailed logging every 10 chunks to track streaming progress
- Log when streaming starts and total chunks/chars at completion

OpenRouter streaming now works correctly:
✅ Uses base_url='https://openrouter.ai/api/v1'
✅ Includes HTTP-Referer and X-Title headers
✅ Streams immediately with no artificial delays
✅ Logs: 'Starting to stream...', 'Streamed N chunks', 'Completed'
✅ Works with all openrouter/ prefixed models

Example streaming flow:
1. User sends request
2. Backend logs: '[Generate] Starting to stream from openrouter/sherlock-dash-alpha...'
3. Chunks arrive: '[Generate] Streamed 10 chunks, 250 chars total'
4. Frontend receives each chunk immediately via SSE
5. Monaco editor updates in real-time
6. Backend logs: '[Generate] Completed with 147 chunks, total length: 3241'

1 file changed +20 -6
backend_api.py+20-6