[llm_core] stream_llm duplicates 3 nearly-identical streaming parser blocks #716

Open
opened 2026-06-03 00:00:20 +02:00 by sleepy · 0 comments
Owner

File: src/llm_core.py function stream_llm (lines 740-1063)

The stream_llm function has three separate streaming parser blocks with near-identical error handling:

  1. Ollama streaming (lines 806-859) — Ollama-native JSON stream
  2. Anthropic streaming (lines 862-952) — SSE with Anthropic-specific events
  3. OpenAI streaming (lines 954-1063) — SSE with OpenAI-compatible chunks

All three blocks share the same error handling pattern:

except (httpx.ConnectError, httpx.ConnectTimeout) as e:
    _cooled = _mark_host_dead(target_url)
    yield f'event: error...'
except httpx.ReadTimeout:
    yield f'event: error...Read timeout...'
except httpx.NetworkError:
    yield f'event: error...Network error...'
except Exception as e:
    yield f'event: error...'

This 20-line error block is copy-pasted 3 times. The only difference between the blocks is the stream parsing logic.

Fix: Extract the error handling into a context manager or decorator. Each provider parser should only implement its unique parsing logic. The connection opening, error handling, and dead-host tracking should be shared.

**File:** `src/llm_core.py` function `stream_llm` (lines 740-1063) The `stream_llm` function has three separate streaming parser blocks with near-identical error handling: 1. **Ollama streaming** (lines 806-859) — Ollama-native JSON stream 2. **Anthropic streaming** (lines 862-952) — SSE with Anthropic-specific events 3. **OpenAI streaming** (lines 954-1063) — SSE with OpenAI-compatible chunks All three blocks share the same error handling pattern: ```python except (httpx.ConnectError, httpx.ConnectTimeout) as e: _cooled = _mark_host_dead(target_url) yield f'event: error...' except httpx.ReadTimeout: yield f'event: error...Read timeout...' except httpx.NetworkError: yield f'event: error...Network error...' except Exception as e: yield f'event: error...' ``` This 20-line error block is copy-pasted 3 times. The only difference between the blocks is the stream parsing logic. **Fix:** Extract the error handling into a context manager or decorator. Each provider parser should only implement its unique parsing logic. The connection opening, error handling, and dead-host tracking should be shared.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
sleepy/odysseus#716
No description provided.