omi icon indicating copy to clipboard operation
omi copied to clipboard

Knowledge graph extraction: no retry/backoff on LLM calls, no fallback on parse failures

Open beastoin opened this issue 7 months ago • 2 comments

Observed

GCP Error Reporting shows three related error patterns in KG extraction:

  • knowledge_graph.py:190 process_memory — 127 hits (single-day burst Apr 11, first seen)
  • knowledge_graph.py:99 extract_knowledge_from_memory — 5 hits/7d (chronic, pydantic validation failure)
  • JSON decode error in LLM output — 10 hits/7d (chronic since Dec 2025, langchain parser)

Code path

Both extract_knowledge_from_memory() (line 98) and process_memory() (line 190) call:

response = llm_mini.invoke(prompt)
extraction = parser.parse(response.content)
  • llm_mini = ChatOpenAI(model='gpt-4.1-mini') with no explicit max_retries or backoff configured (clients.py:23)
  • PydanticOutputParser.parse() has no fallback — if LLM returns malformed JSON or non-JSON text, it raises immediately
  • process_memory() runs inside ThreadPoolExecutor during rebuild_knowledge_graph() — concurrent failures amplify during API instability

Failure modes

  1. OpenAI API transient failure → llm_mini.invoke() raises, no retry → error logged, KG extraction skipped for that memory
  2. LLM returns malformed JSON → parser.parse() raises pydantic ValidationError → same outcome
  3. Burst pattern (127 hits on Apr 11) → likely OpenAI outage, but zero retry means every single call fails instead of recovering

Impact

KG extraction silently fails for affected memories. The memory gets kg_extracted: False and may or may not be retried on next conversation processing cycle.

Data source

GCP Error Reporting, backend service, last 7 days. Burst on Apr 11, chronic low-level since Dec 2025.

beastoin avatar Apr 12 '26 00:04 beastoin

Is it possible to make extraction a retryable processing step instead of a one-shot inline call?

Save the raw memory first, then run KG extraction with clear states like queued, done, and failed. That would make transient OpenAI errors or malformed JSON responses recoverable instead of leaving the memory permanently unextracted.

If the facts are already structured at any point in the flow, we could also write them directly instead of asking the model to reconstruct them from text.

Roughly:

memory = save_memory(raw_text, kg_status="queued")

try:
    response = llm_mini.invoke(prompt)
    extraction = parser.parse(response.content)
    write_knowledge_graph(memory.id, extraction)
    mark_memory(memory.id, kg_status="done")
except (OpenAIError, ValidationError, JSONDecodeError) as e:
    mark_memory(
        memory.id,
        kg_status="failed",
        kg_error_type=type(e).__name__,
        kg_retryable=is_retryable(e),
    )

Then a background job can retry only the retryable failures:

for memory in get_memories(kg_status="failed", kg_retryable=True):
    retry_kg_extraction(memory)

This would avoid permanently losing KG extraction because of one temporary OpenAI failure or one malformed JSON response. The memory still gets saved, the failure is visible, and extraction can be retried without reprocessing the whole conversation.

darthvader58 avatar Apr 15 '26 09:04 darthvader58

Addressed by merged PR(s): #6705

beastoin avatar May 10 '26 06:05 beastoin