hindsight icon indicating copy to clipboard operation
hindsight copied to clipboard

NoneType content from OpenRouter causes 'NoneType' object is not subscriptable in retain and consolidation (v0.5.6)

Open downwind7clawd-ctrl opened this issue 7 months ago • 1 comments

Bug Description

When using OpenRouter as the LLM provider (e.g. openrouter/nvidia/nemotron-3-super-120b-a12b:free), the OpenAICompatibleLLM.call() method occasionally receives response.choices[0].message.content == None. The code does not check for None before passing content to _strip_code_fences(), which expects a string, causing:

TypeError: 'NoneType' object is not subscriptable

This affects both retain (fact extraction) and consolidation operations. Retries (up to 11 for retain, 3 for consolidation) do not help because the provider consistently returns null content.

Environment

  • hindsight-api / hindsight-api-slim: 0.5.6
  • Provider: openrouter
  • Model: nvidia/nemotron-3-super-120b-a12b:free (also reproduced with openai/gpt-oss-120b:free)
  • Response format: {"type": "json_object"} (soft enforcement via response_format parameter)
  • OS: Ubuntu 22.04, Python 3.11
  • Install method: pip in venv (~/.hermes/hermes-agent/venv/)

Root Cause

In openai_compatible_llm.py line ~375:

content = response.choices[0].message.content  # Can be None
# ...
clean_content = _strip_code_fences(content)     # TypeError if content is None

_strip_code_fences() calls operations that expect a string (e.g. "```" in content), but content can be None when the LLM returns an empty/null response.

Expected Behavior

When content is None:

  1. Log a warning with model/provider details
  2. Retry the LLM call (with backoff)
  3. If all retries exhausted, return empty default (e.g. {}) or raise a clear error

Actual Behavior

TypeError: 'NoneType' object is not subscriptable crashes the fact extraction or consolidation task, wasting the retry budget on the same unhandled error.

Logs

ERROR - Chunk 0/1 extraction failed after 3 attempts: TypeError: 'NoneType' object is not subscriptable
WARNING - Content extraction failed (skipping): RuntimeError: Fact extraction failed: 1/1 chunks failed after 3 retries each.
INFO - STREAMING RETAIN COMPLETE: 0 units across 1 batches in 238.698s

WARNING - [CONSOLIDATION] LLM batch call failed (attempt 3/3): 'NoneType' object is not subscriptable
ERROR - [CONSOLIDATION] LLM batch call failed after 3 attempts, skipping batch.

INFO - [WORKER_TASK] [STUCK?] op=... type=consolidation bank=hermes age=1225s stage=llm.openrouter.consolidation+structured

Suggested Fix

Add a null guard after extracting content:

content = response.choices[0].message.content
if not content:
    logger.warning(f"LLM returned null/empty content for {self.provider}/{self.model}")
    # retry or return empty default
    ...

downwind7clawd-ctrl avatar Apr 29 '26 15:04 downwind7clawd-ctrl

DeepWiki Analysis (Supplementary)

Queried DeepWiki on this repository for additional context on the bug. Key findings:

Error Location (Confirmed)

  • **** — content = response.choices[0].message.content where content can be None
  • **** — clean_content = _strip_code_fences(content) passes None to a function expecting str

Root Cause in _strip_code_fences()

The function at openai_compatible_llm.py:45-61 performs if "```" not in content without a type guard. When content is None, this raises AttributeError (or TypeError depending on the operation).

Affected Code Paths

  • Retain: fact_extraction.py:1012-1023 — fact extraction LLM calls
  • Consolidation: fact_extraction.py:1755-1768 — batch JSON parsing

Why This Is Not Provider-Specific

Per the OpenAI API spec, message.content is explicitly nullable. Any provider (not just OpenRouter) can return content=None under conditions like:

  • Server overload / timeout
  • Token limit reached (reasoning tokens consume the budget)
  • Content filtering / safety refusal
  • Free tier throttling

Suggested Fix (Two-Layer Defense)

  1. Caller-level guard (as suggested in the original report):
content = response.choices[0].message.content
if not content:
    logger.warning(f"LLM returned null/empty content for {self.provider}/{self.model}")
    content = ""
  1. Function-level guard (additional hardening):
def _strip_code_fences(content: str) -> str:
    if not content or "```" not in content:
        return content or ""
    # ... rest of logic

Both layers are recommended — the caller guard provides logging for observability, while the function guard prevents similar crashes from any future call site.

downwind7clawd-ctrl avatar Apr 29 '26 23:04 downwind7clawd-ctrl