Knowledge graph extraction: no retry/backoff on LLM calls, no fallback on parse failures
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 explicitmax_retriesor backoff configured (clients.py:23)PydanticOutputParser.parse()has no fallback — if LLM returns malformed JSON or non-JSON text, it raises immediatelyprocess_memory()runs insideThreadPoolExecutorduringrebuild_knowledge_graph()— concurrent failures amplify during API instability
Failure modes
- OpenAI API transient failure →
llm_mini.invoke()raises, no retry → error logged, KG extraction skipped for that memory - LLM returns malformed JSON →
parser.parse()raises pydanticValidationError→ same outcome - 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.
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.
Addressed by merged PR(s): #6705