Skip to content

Fix schema validation #345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,65 +42,67 @@
}
},
"from_pdf": false,
"entities": [
"Person",
{
"label": "House",
"description": "Family the person belongs to",
"properties": [
{
"name": "name",
"type": "STRING"
}
]
},
{
"label": "Planet",
"properties": [
{
"name": "name",
"type": "STRING"
},
{
"name": "weather",
"type": "STRING"
}
]
}
],
"relations": [
"PARENT_OF",
{
"label": "HEIR_OF",
"description": "Used for inheritor relationship between father and sons"
},
{
"label": "RULES",
"properties": [
{
"name": "fromYear",
"type": "INTEGER"
}
]
}
],
"potential_schema": [
[
"schema": {
"node_types": [
"Person",
"PARENT_OF",
"Person"
{
"label": "House",
"description": "Family the person belongs to",
"properties": [
{
"name": "name",
"type": "STRING"
}
]
},
{
"label": "Planet",
"properties": [
{
"name": "name",
"type": "STRING"
},
{
"name": "weather",
"type": "STRING"
}
]
}
],
[
"Person",
"HEIR_OF",
"House"
"relationship_types": [
"PARENT_OF",
{
"label": "HEIR_OF",
"description": "Used for inheritor relationship between father and sons"
},
{
"label": "RULES",
"properties": [
{
"name": "fromYear",
"type": "INTEGER"
}
]
}
],
[
"House",
"RULES",
"Planet"
"patterns": [
[
"Person",
"PARENT_OF",
"Person"
],
[
"Person",
"HEIR_OF",
"House"
],
[
"House",
"RULES",
"Planet"
]
]
],
},
"text_splitter": {
"class_": "text_splitters.fixed_size_splitter.FixedSizeSplitter",
"params_": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,32 @@ embedder_config:
resolver_: ENV
var_: OPENAI_API_KEY
from_pdf: false
entities:
- label: Person
- label: House
description: Family the person belongs to
properties:
- name: name
type: STRING
- label: Planet
properties:
- name: name
type: STRING
- name: weather
type: STRING
relations:
- label: PARENT_OF
- label: HEIR_OF
description: Used for inheritor relationship between father and sons
- label: RULES
properties:
- name: fromYear
type: INTEGER
potential_schema:
- ["Person", "PARENT_OF", "Person"]
- ["Person", "HEIR_OF", "House"]
- ["House", "RULES", "Planet"]
schema:
node_types:
- label: Person
- label: House
description: Family the person belongs to
properties:
- name: name
type: STRING
- label: Planet
properties:
- name: name
type: STRING
- name: weather
type: STRING
relationship_types:
- label: PARENT_OF
- label: HEIR_OF
description: Used for inheritor relationship between father and sons
- label: RULES
properties:
- name: fromYear
type: INTEGER
patterns:
- ["Person", "PARENT_OF", "Person"]
- ["Person", "HEIR_OF", "House"]
- ["House", "RULES", "Planet"]
text_splitter:
class_: text_splitters.fixed_size_splitter.FixedSizeSplitter
params_:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,65 +42,67 @@
}
},
"from_pdf": true,
"entities": [
"Person",
{
"label": "House",
"description": "Family the person belongs to",
"properties": [
{
"name": "name",
"type": "STRING"
}
]
},
{
"label": "Planet",
"properties": [
{
"name": "name",
"type": "STRING"
},
{
"name": "weather",
"type": "STRING"
}
]
}
],
"relations": [
"PARENT_OF",
{
"label": "HEIR_OF",
"description": "Used for inheritor relationship between father and sons"
},
{
"label": "RULES",
"properties": [
{
"name": "fromYear",
"type": "INTEGER"
}
]
}
],
"potential_schema": [
[
"schema": {
"node_types": [
"Person",
"PARENT_OF",
"Person"
{
"label": "House",
"description": "Family the person belongs to",
"properties": [
{
"name": "name",
"type": "STRING"
}
]
},
{
"label": "Planet",
"properties": [
{
"name": "name",
"type": "STRING"
},
{
"name": "weather",
"type": "STRING"
}
]
}
],
[
"Person",
"HEIR_OF",
"House"
"relationship_types": [
"PARENT_OF",
{
"label": "HEIR_OF",
"description": "Used for inheritor relationship between father and sons"
},
{
"label": "RULES",
"properties": [
{
"name": "fromYear",
"type": "INTEGER"
}
]
}
],
[
"House",
"RULES",
"Planet"
"patterns": [
[
"Person",
"PARENT_OF",
"Person"
],
[
"Person",
"HEIR_OF",
"House"
],
[
"House",
"RULES",
"Planet"
]
]
],
},
"text_splitter": {
"class_": "text_splitters.fixed_size_splitter.FixedSizeSplitter",
"params_": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ async def extract_for_chunk(
) -> Neo4jGraph:
"""Run entity extraction for a given text chunk."""
prompt = self.prompt_template.format(
text=chunk.text, schema=schema.model_dump(), examples=examples
text=chunk.text,
schema=schema.model_dump(exclude_none=True),
examples=examples,
)
llm_result = await self.llm.ainvoke(prompt)
try:
Expand Down Expand Up @@ -326,7 +328,7 @@ async def run(
elif lexical_graph_config:
lexical_graph_builder = LexicalGraphBuilder(config=lexical_graph_config)
schema = schema or GraphSchema(
node_types=(), relationship_types=(), patterns=()
node_types=(), relationship_types=None, patterns=None
)
examples = examples or ""
sem = asyncio.Semaphore(self.max_concurrency)
Expand Down
22 changes: 10 additions & 12 deletions src/neo4j_graphrag/experimental/components/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,12 @@ class NodeType(BaseModel):
description: str = ""
properties: list[PropertyType] = []

@model_validator(mode="before")
@classmethod
def from_text_or_dict(cls, input: EntityInputType) -> Self:
if isinstance(input, NodeType):
return input
if isinstance(input, str):
return cls(label=input)
return cls.model_validate(input)
def validate_input_if_string(cls, data: EntityInputType) -> EntityInputType:
if isinstance(data, str):
return {"label": data}
return data


class RelationshipType(BaseModel):
Expand All @@ -99,13 +98,12 @@ class RelationshipType(BaseModel):
description: str = ""
properties: list[PropertyType] = []

@model_validator(mode="before")
@classmethod
def from_text_or_dict(cls, input: RelationInputType) -> Self:
if isinstance(input, RelationshipType):
return input
if isinstance(input, str):
return cls(label=input)
return cls.model_validate(input)
def validate_input_if_string(cls, data: RelationInputType) -> RelationInputType:
if isinstance(data, str):
return {"label": data}
return data


class GraphSchema(DataModel):
Expand Down
Loading
Loading