#1909·graphiti

OpenAIGenericClient: json_schema response_format lets grammar backends skip optional keys, so extracted edges come back without valid_at/invalid_at (Ollama, llama.cpp)

Author: v8etaCreated Sep 23, 2026Updated Sep 30, 2026

Summary

OpenAIGenericClient._build_response_format sends response_model.model_json_schema() as the json_schema response format. pydantic leaves every field that has a default out of required. Servers that compile the schema into a decoding grammar (llama.cpp, and Ollama on top of it) treat required as "must emit" and everything else as "may skip", so a small model under the grammar tends to omit optional keys entirely instead of writing null. For ExtractedEdge, valid_at and invalid_at are str | None = None, so the effect is edges with no dates even when the prompt's rules (REFERENCE_TIME, "use January 1st") apply.

The class docstring says json_schema mode "uses constrained decoding"; the schema is enforced, but the shape being enforced is one in which the date keys are optional. The dedicated OpenAIClient does not have this problem: responses.parse() produces OpenAI's strict shape, in which every property is required.

Measurements

Setup: graphiti-core 0.30.1 (MCP server image zepai/knowledge-graph-mcp:1.1.0-graphiti-0.30.1), OpenAIGenericClient → Ollama 0.34.3 (llama.cpp backend) on one 16 GB card, structured_output_mode='json_schema', temperature 0.2, five frozen operations-log episodes (750–1,900 chars) with explicit reference_time, a 45-fact gold set graded by hand.

Model Schema as sent today Every property listed as required
gemma3:12b (Q4_K_M) 10 of 40 edges with valid_at null 0 of 72
phi4:14b (Q4_K_M) 0 of 39 (this model happened to always emit the key) 0 of 44

Same prompts, same episodes, same reference times. With the keys always present, the model wrote null where it had no date and the reference-day date otherwise; the run on gemma3:12b also went from 28 to 37 of 45 gold facts found, though that gain includes other changes made at the same time (caller-side custom_extraction_instructions), so I am not attributing it to this alone.

How the omission was confirmed rather than inferred: llama.cpp's json-schema-to-grammar emits required properties first and makes each non-required property an optional grammar branch; re-sending the identical request with required extended to all properties made the keys appear on every edge.

Expected behaviour

In json_schema mode the sent schema should ask the server for the same shape the strict OpenAI path already produces: every property of every object listed as required. Types stay as pydantic wrote them, so a str | None field is still answered with null and response validation is unchanged. additionalProperties: false is not needed for this and is left alone.

Proposed fix

#1910: a small recursive helper in OpenAIGenericClient that lists all properties of every object schema as required before sending (walks $defs, items, anyOf; leaves objects without properties untouched), applied in _build_response_format. Tests cover the transformation, non-mutation of the input schema, and that validation still accepts a reply with the optional keys missing. make check passes.

Versions

  • graphiti-core 0.30.1 (the same code is on main at 16cdf70 — _build_response_format there sends model_json_schema() unchanged)
  • pydantic 2.13.5, openai 3.6.0
  • Ollama 0.34.3 (llama.cpp), models gemma3:12b and phi4:14b, Q4_K_M
  • Python 3.13

Related observations from the same runs (not part of the fix above; happy to split them into their own issues)

  1. A state change attached to a different subject is never retired. With "the verifier runs in log-only mode" extracted from episode 1 (edge on llm-auth) and "the router moved from log-only to enforce" from episode 4 (edge on the router), no invalidation happens: the scoped contradiction check compares edges between the same entity pair, so the two facts never meet. Both models produced this shape. This is adjacent to #1728 / #1729 rather than caused by them.
  2. Exact-name matching in extract_edges drops edges on small models. The LLM's source_entity_name / target_entity_name must equal a node name exactly; phi4:14b lost 6 of 10 extracted edges on one episode this way (vault data vs vault, trailing periods, quotes). A case/quote/whitespace fold plus a unique-prefix match before the drop recovered them in my patched build; a logged count of dropped edges per episode would at least make the loss visible.

References that shaped the fix

  • llama.cpp grammars/README.md and examples/json_schema_to_grammar.py: required properties are emitted first; non-required properties are optional branches.
  • Ollama, "Structured outputs" documentation: recommends passing the schema in the prompt as well as in format.
  • Tam et al., "Let Me Speak Freely?" (2024) and the dottxt reply "Say What You Mean" (2024): constrained decoding does not hurt quality when the prompt and the schema agree; the failure modes are in the mismatch.
  • Geng et al., "Generating Structured Outputs from Language Models: Benchmark and Studies" (JSONSchemaBench, arXiv:2501.10868): coverage of optional/required handling across engines.