ATH-MaaS/Pixelle-Video · error · ValueError

Failed to parse LLM response as {response_type.__name__}: {c

Error message

Failed to parse LLM response as {response_type.__name__}: {content[:200]}...

What it means

LLMService._parse_response_as_model tries three ways to recover JSON from the LLM's raw text — direct json.loads, a ```json fenced block, and a brace-substring extraction — then validates with response_type.model_validate (Pydantic). If none parse as JSON it raises ValueError naming the target model type and a 200-char snippet of the content.

Source

Thrown at pixelle_video/services/llm_service.py:330

        if match:
            try:
                data = json.loads(match.group(1))
                return response_type.model_validate(data)
            except json.JSONDecodeError:
                pass
        
        # Try to find any JSON object in the text
        brace_start = content.find('{')
        brace_end = content.rfind('}')
        if brace_start != -1 and brace_end > brace_start:
            try:
                json_str = content[brace_start:brace_end + 1]
                data = json.loads(json_str)
                return response_type.model_validate(data)
            except json.JSONDecodeError:
                pass
        
        raise ValueError(f"Failed to parse LLM response as {response_type.__name__}: {content[:200]}...")
    
    @property
    def active(self) -> str:
        """
        Get active model name
        
        Returns:
            Active model name
        
        Example:
            print(f"Using model: {pixelle_video.llm.active}")
        """
        return self._get_config_value("model", "gpt-3.5-turbo")
    
    def __repr__(self) -> str:
        """String representation"""
        model = self.active
        base_url = self._get_config_value("base_url", "default")

View on GitHub (pinned to 848b054e4f)

Solutions

  1. Strengthen the prompt: demand strict JSON only, no prose, and provide the schema inline or via function-calling/JSON mode
  2. Increase max_tokens so the JSON is not truncated mid-object
  3. Use a provider-native structured-output/JSON mode or function calling instead of prompt-based JSON
  4. Retry with a fallback model that follows JSON instructions reliably
  5. Loosen/repair parsing (e.g. strip trailing commas, use json-repair) before failing

Example fix

// before
text = await llm.generate(prompt)
scene = Scene.model_validate_json(text)  # may raise ValueError
// after
prompt = base_prompt + "\nRespond with ONLY a JSON object matching the schema. No markdown, no commentary."
try:
    scene = await llm.parse(prompt, Scene)  # structured output mode
except ValueError:
    scene = await fallback_llm.parse(prompt, Scene)
Defensive patterns

Strategy: retry

Validate before calling

import json, re
def looks_like_json(content: str) -> bool:
    m = re.search(r'\{[\s\S]*\}', content)
    if not m:
        return False
    try:
        json.loads(m.group(0))
        return True
    except json.JSONDecodeError:
        return False

Type guard

def is_parseable_as(content: str, model) -> bool:
    from pydantic import ValidationError
    m = re.search(r'\{[\s\S]*\}', content)
    if not m:
        return False
    try:
        model.model_validate(json.loads(m.group(0)))
        return True
    except (json.JSONDecodeError, ValidationError):
        return False

Try / catch

for attempt in range(3):
    try:
        return await llm.parse(prompt, Scene)
    except ValueError:
        prompt += "\nIMPORTANT: reply with a single valid JSON object only."
raise RuntimeError("LLM never returned parseable JSON")

Prevention

When it happens

Trigger: Calling _call_with_structured_output with a Pydantic model and the model returned prose, a refusal, truncated JSON, or JSON with trailing text that broke parsing; note that JSONDecodeError is caught but Pydantic ValidationError propagates differently, so this specifically means 'no parseable JSON object found'.

Common situations: Small/cheap model that ignores the JSON instruction; response truncated by max_tokens mid-object; model wraps JSON in prose or emits single quotes/JS-style literals; safety refusal instead of data.

Understand the failure class

Related errors


AI-assisted analysis of ATH-MaaS/Pixelle-Video@848b054e4f (2026-08-30). Data as JSON: /api/errors/7e7bd78b5877679b. Report an issue: GitHub.