Document Ingestion
Document Retrieval
Data Organization
Document Updates
Batch Operations
Knowledge Graph Operations
Cache Management
update_graph
Update an existing knowledge graph with new documents
Usage
from morphik import Morphik
db = Morphik()
# Update a graph with additional documents
updated_graph = db.update_graph(
name="research_graph",
additional_filters={"category": "new_research"},
additional_documents=["doc_123", "doc_456"]
)
print(f"Graph now has {len(updated_graph.entities)} entities")
print(f"Graph now has {len(updated_graph.relationships)} relationships")
from morphik import Morphik
db = Morphik()
# Update a graph with additional documents
updated_graph = db.update_graph(
name="research_graph",
additional_filters={"category": "new_research"},
additional_documents=["doc_123", "doc_456"]
)
print(f"Graph now has {len(updated_graph.entities)} entities")
print(f"Graph now has {len(updated_graph.relationships)} relationships")
from morphik import AsyncMorphik
async with AsyncMorphik() as db:
# Update a graph with additional documents
updated_graph = await db.update_graph(
name="research_graph",
additional_filters={"category": "new_research"},
additional_documents=["doc_123", "doc_456"]
)
print(f"Graph now has {len(updated_graph.entities)} entities")
print(f"Graph now has {len(updated_graph.relationships)} relationships")
Parameters
name
(str): Name of the graph to updateadditional_filters
(Dict[str, Any], optional): Optional additional metadata filters to determine which new documents to includeadditional_documents
(List[str], optional): Optional list of additional document IDs to includeprompt_overrides
(GraphPromptOverrides | Dict[str, Any], optional): Optional customizations for entity extraction and resolution prompts
Returns
A Graph object representing the updated knowledge graph.
Description
This method processes additional documents matching the original or new filters, extracts entities and relationships, and updates the graph with new information.
The graph update operation:
- Retrieves additional documents based on filters and/or specific document IDs
- Extracts entities and relationships from these documents
- Intelligently merges new entities and relationships with the existing graph
- Returns the updated graph with all entities and relationships
Advanced Examples
With Entity Resolution Examples
from morphik.models import EntityResolutionPromptOverride, EntityResolutionExample, GraphPromptOverrides
# Update with custom entity resolution examples
updated_graph = db.update_graph(
name="research_graph",
additional_documents=["doc_123"],
prompt_overrides=GraphPromptOverrides(
entity_resolution=EntityResolutionPromptOverride(
examples=[
EntityResolutionExample(
canonical="Machine Learning",
variants=["ML", "machine learning", "AI/ML"]
),
EntityResolutionExample(
canonical="Natural Language Processing",
variants=["NLP", "natural language processing", "text processing"]
)
]
)
)
)
# With custom entity resolution prompt template
updated_graph = db.update_graph(
name="research_graph",
additional_filters={"year": "2025"},
prompt_overrides=GraphPromptOverrides(
entity_resolution=EntityResolutionPromptOverride(
prompt_template="""I have extracted the following entities from the text:
{entities_str}
Here are examples of how different entity references can be grouped together:
{examples_json}
Please resolve these entities by identifying which mentions refer to the same entity.
Group them together, selecting a canonical/preferred form for each group.
Return your resolution in JSON format with the canonical form and all its variants.
""",
examples=[
EntityResolutionExample(
canonical="General AI",
variants=["AGI", "General Artificial Intelligence", "General AI"]
)
]
)
)
)
from morphik.models import EntityResolutionPromptOverride, EntityResolutionExample, GraphPromptOverrides
# Update with custom entity resolution examples
updated_graph = db.update_graph(
name="research_graph",
additional_documents=["doc_123"],
prompt_overrides=GraphPromptOverrides(
entity_resolution=EntityResolutionPromptOverride(
examples=[
EntityResolutionExample(
canonical="Machine Learning",
variants=["ML", "machine learning", "AI/ML"]
),
EntityResolutionExample(
canonical="Natural Language Processing",
variants=["NLP", "natural language processing", "text processing"]
)
]
)
)
)
# With custom entity resolution prompt template
updated_graph = db.update_graph(
name="research_graph",
additional_filters={"year": "2025"},
prompt_overrides=GraphPromptOverrides(
entity_resolution=EntityResolutionPromptOverride(
prompt_template="""I have extracted the following entities from the text:
{entities_str}
Here are examples of how different entity references can be grouped together:
{examples_json}
Please resolve these entities by identifying which mentions refer to the same entity.
Group them together, selecting a canonical/preferred form for each group.
Return your resolution in JSON format with the canonical form and all its variants.
""",
examples=[
EntityResolutionExample(
canonical="General AI",
variants=["AGI", "General Artificial Intelligence", "General AI"]
)
]
)
)
)
from morphik.models import EntityResolutionPromptOverride, EntityResolutionExample, GraphPromptOverrides
# Update with custom entity resolution examples
updated_graph = await db.update_graph(
name="research_graph",
additional_documents=["doc_123"],
prompt_overrides=GraphPromptOverrides(
entity_resolution=EntityResolutionPromptOverride(
examples=[
EntityResolutionExample(
canonical="Machine Learning",
variants=["ML", "machine learning", "AI/ML"]
),
EntityResolutionExample(
canonical="Natural Language Processing",
variants=["NLP", "natural language processing", "text processing"]
)
]
)
)
)
# With custom entity resolution prompt template
updated_graph = await db.update_graph(
name="research_graph",
additional_filters={"year": "2025"},
prompt_overrides=GraphPromptOverrides(
entity_resolution=EntityResolutionPromptOverride(
prompt_template="""I have extracted the following entities from the text:
{entities_str}
Here are examples of how different entity references can be grouped together:
{examples_json}
Please resolve these entities by identifying which mentions refer to the same entity.
Group them together, selecting a canonical/preferred form for each group.
Return your resolution in JSON format with the canonical form and all its variants.
""",
examples=[
EntityResolutionExample(
canonical="General AI",
variants=["AGI", "General Artificial Intelligence", "General AI"]
)
]
)
)
)
With Entity Extraction Examples
from morphik.models import EntityExtractionPromptOverride, EntityExtractionExample, GraphPromptOverrides
# Update with custom entity extraction examples
updated_graph = db.update_graph(
name="medical_graph",
additional_filters={"category": "new_medical_data"},
prompt_overrides=GraphPromptOverrides(
entity_extraction=EntityExtractionPromptOverride(
examples=[
EntityExtractionExample(label="Insulin", type="MEDICATION"),
EntityExtractionExample(label="Diabetes", type="CONDITION"),
EntityExtractionExample(label="Heart rate", type="VITAL_SIGN"),
EntityExtractionExample(label="Cardiology", type="SPECIALTY")
]
)
)
)
# With custom entity extraction template
updated_graph = db.update_graph(
name="legal_graph",
additional_documents=["contract1", "contract2"],
prompt_overrides=GraphPromptOverrides(
entity_extraction=EntityExtractionPromptOverride(
prompt_template="""Extract legal entities from the following document:
{content}
Focus on these types of entities:
{examples}
Return the extracted entities in JSON format with the following structure:
[
{"label": "entity name", "type": "ENTITY_TYPE", "properties": {"key": "value"}}
]
""",
examples=[
EntityExtractionExample(
label="John Smith",
type="PERSON",
properties={"role": "Plaintiff"}
),
EntityExtractionExample(
label="Acme Corporation",
type="ORGANIZATION",
properties={"type": "Corporation"}
),
EntityExtractionExample(
label="January 15, 2025",
type="DATE"
)
]
)
)
)
from morphik.models import EntityExtractionPromptOverride, EntityExtractionExample, GraphPromptOverrides
# Update with custom entity extraction examples
updated_graph = db.update_graph(
name="medical_graph",
additional_filters={"category": "new_medical_data"},
prompt_overrides=GraphPromptOverrides(
entity_extraction=EntityExtractionPromptOverride(
examples=[
EntityExtractionExample(label="Insulin", type="MEDICATION"),
EntityExtractionExample(label="Diabetes", type="CONDITION"),
EntityExtractionExample(label="Heart rate", type="VITAL_SIGN"),
EntityExtractionExample(label="Cardiology", type="SPECIALTY")
]
)
)
)
# With custom entity extraction template
updated_graph = db.update_graph(
name="legal_graph",
additional_documents=["contract1", "contract2"],
prompt_overrides=GraphPromptOverrides(
entity_extraction=EntityExtractionPromptOverride(
prompt_template="""Extract legal entities from the following document:
{content}
Focus on these types of entities:
{examples}
Return the extracted entities in JSON format with the following structure:
[
{"label": "entity name", "type": "ENTITY_TYPE", "properties": {"key": "value"}}
]
""",
examples=[
EntityExtractionExample(
label="John Smith",
type="PERSON",
properties={"role": "Plaintiff"}
),
EntityExtractionExample(
label="Acme Corporation",
type="ORGANIZATION",
properties={"type": "Corporation"}
),
EntityExtractionExample(
label="January 15, 2025",
type="DATE"
)
]
)
)
)
from morphik.models import EntityExtractionPromptOverride, EntityExtractionExample, GraphPromptOverrides
# Update with custom entity extraction examples
updated_graph = await db.update_graph(
name="medical_graph",
additional_filters={"category": "new_medical_data"},
prompt_overrides=GraphPromptOverrides(
entity_extraction=EntityExtractionPromptOverride(
examples=[
EntityExtractionExample(label="Insulin", type="MEDICATION"),
EntityExtractionExample(label="Diabetes", type="CONDITION"),
EntityExtractionExample(label="Heart rate", type="VITAL_SIGN"),
EntityExtractionExample(label="Cardiology", type="SPECIALTY")
]
)
)
)
# With custom entity extraction template
updated_graph = await db.update_graph(
name="legal_graph",
additional_documents=["contract1", "contract2"],
prompt_overrides=GraphPromptOverrides(
entity_extraction=EntityExtractionPromptOverride(
prompt_template="""Extract legal entities from the following document:
{content}
Focus on these types of entities:
{examples}
Return the extracted entities in JSON format with the following structure:
[
{"label": "entity name", "type": "ENTITY_TYPE", "properties": {"key": "value"}}
]
""",
examples=[
EntityExtractionExample(
label="John Smith",
type="PERSON",
properties={"role": "Plaintiff"}
),
EntityExtractionExample(
label="Acme Corporation",
type="ORGANIZATION",
properties={"type": "Corporation"}
),
EntityExtractionExample(
label="January 15, 2025",
type="DATE"
)
]
)
)
)
Notes
- The graph name must match an existing graph that the user has access to.
- Either
additional_filters
oradditional_documents
(or both) should be provided; otherwise, no new content will be added to the graph. - When using
additional_filters
, these are applied in addition to any filters used during graph creation. - The
prompt_overrides
are applied only to this update operation and do not permanently change the configuration of the graph.
Was this page helpful?