Non-string top-level key in facts or schema raises a bare TypeError in Python

Aperta Adatta ai principianti
#392 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
77/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Inizia da src/selfgraph/models.py, in particolare da as_input e _reject_unknown_keys, e leggi il contratto degli errori in AGENTS.md. Riproduci il problema con una chiave di primo livello non stringa in graph.insert.facts o as_schema, poi trova il test «every code» menzionato nell’issue. Il lavoro è concluso quando questi input generano un ValidationError con il codice unknown_key e il test copre questo comportamento.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

approved-for-pr area: errors area: write bug effort: S P3

In Python, a facts or schema mapping whose top level has a key that is not a string raises a bare TypeError from pydantic instead of a coded ValidationError. The same bad key one level down gets a proper unknown_key error.

Tested at 5a277c9 (current main), Kuzu.

Reproduce
await graph.insert.facts({1: "x"})
# TypeError: keywords must be strings

await graph.insert.facts({None: []})
# TypeError: keywords must be strings

from selfgraph.schema import as_schema
as_schema({1: "x"})          # also SelfGraph(store, schema={1: "x"})
# TypeError: keywords must be strings

For comparison:

await graph.insert.facts({"entities": [{1: "x"}]})
# ValidationError[unknown_key]: unknown key '1' in facts.entities[0] (valid keys: id, type, name, ...)

as_input(ExtractionInstructions, {1: "x"}, "instructions")
# ValidationError[unknown_key]: unknown key '1' in instructions (valid keys: entities, relations, preferences, decisions)
Cause

as_input (src/selfgraph/models.py:306) calls model.model_validate(value). For the top-level input models (Extraction, Schema), which override __init__, pydantic passes the mapping on as keyword arguments, and Python rejects a non-string keyword before any validator runs. The unknown-key check in _reject_unknown_keys (models.py:270), which names the bad key for nested models and for the instructions and prompts inputs, never sees it.

The TypeScript SDK and the CLI and MCP paths cannot produce a non-string key, so only direct Python API callers reach this. Per the error contract in AGENTS.md, every failure should carry a code.

Suggested fix

Check that the top-level keys are strings in as_input (or in TopInputModel before calling super().__init__) and raise the same unknown_key ValidationError the nested models give, then add the call to the "every code" test.

Lingua principale
TypeScript
Stelle
173
Fork
58
Merge medio
6h
PR unite (30g)
120

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di hydra-db/selfgraph

Tutte le issue di hydra-db/selfgraph

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.