Skip to content

Add error handling for LLM response parsing #43

@davidpoblador

Description

@davidpoblador

Summary

The LLM module directly accesses dictionary keys without error handling, which will crash if the response format differs from expected.

Current code

src/blogtuner/ai/llm.py:48-50:

response = model.prompt(prompt, schema=MarkdDownContent)
markdown_content = json.loads(response.text())["markdown"]
return markdown_content

Issues

  1. No try/except around json.loads() - will crash on invalid JSON
  2. Direct ["markdown"] access - will crash if key is missing
  3. No validation that response matches expected schema

Proposed change

try:
    response = model.prompt(prompt, schema=MarkdDownContent)
    data = json.loads(response.text())
    markdown_content = data.get("markdown")
    if markdown_content is None:
        raise ValueError("LLM response missing 'markdown' field")
    return markdown_content
except json.JSONDecodeError as e:
    raise ValueError(f"Invalid JSON response from LLM: {e}") from e

Priority

High

File

src/blogtuner/ai/llm.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions