Validate LLM API response fields to prevent silent failures - #30
Conversation
There was a problem hiding this comment.
Checked the diff against the stated intent: it adds a _validate_response helper and applies it to the non-streaming chat() and generate() paths, which is exactly what the title/description promise, with no unrelated changes bundled in.
The one real behavior change is that chat() now raises OllamaError instead of returning a dict without message — that's already caught at agent/core/engine.py:89 and surfaced as a user-facing "LLM Error", so it fails loudly rather than silently looping on empty content until max_turns. Streaming is correctly left unvalidated since _stream_chat builds full_response with a message key itself, the helper is mypy-safe against _request's -> dict signature, and no existing test mocks LLMClient.chat directly (they patch _call_llm).
Minor, non-blocking: the error interpolates the entire response dict via Got: {response}, which could be a large blob in the terminal — a truncated repr would read better.
What
Add validation to check that LLM API responses contain expected fields before accessing them.
Why
Malformed or unexpected API responses can cause silent failures when required fields are missing, making debugging difficult. This adds explicit validation and clear error messages.