middleware.rdf_vocabulary_middleware

LangChain middleware and tools for search-first RDF vocabulary retrieval.

Attributes

logger

LIST_VOCABULARIES_TOOL_DESCRIPTION

LIST_TERMS_TOOL_DESCRIPTION

SEARCH_TERMS_TOOL_DESCRIPTION

INSPECT_TERM_TOOL_DESCRIPTION

Exceptions

VocabularyNamespaceNotAllowed

Raised when a vocabulary namespace is outside the injected whitelist.

IndexedVocabularyNamespaceNotFound

Raised when a whitelisted namespace has no indexed vocabulary in this run.

IndexedVocabularyTermNotFound

Raised when a term IRI is not present in the indexed vocabularies.

Classes

VocabularySummary

Compact summary of one indexed vocabulary exposed to tools.

VocabularyListResponse

Tool response listing indexed vocabularies available in this run.

ListTermsRequest

Inputs for paginated listing within one indexed vocabulary.

InspectTermRequest

Inputs for inspecting one indexed vocabulary term.

TermInspectionResponse

Detailed inspection payload for one indexed vocabulary term.

RDFVocabularyMiddlewareConfig

Configuration for the RDF vocabulary middleware surface.

RDFVocabularyMiddleware

Expose vocabulary tools and guard repeated static lookups via middleware hooks.

Functions

get_transitive_path(...)

Collect transitive predicate edges reachable upward from leaf_node.

Module Contents

middleware.rdf_vocabulary_middleware.logger
middleware.rdf_vocabulary_middleware.LIST_VOCABULARIES_TOOL_DESCRIPTION: Final[str] = Multiline-String
Show Value
"""List the RDF vocabularies indexed by this middleware.

Use this tool when you do not know which vocabularies are indexed and you want to discover them.

Call this tool with no arguments.
"""
middleware.rdf_vocabulary_middleware.LIST_TERMS_TOOL_DESCRIPTION: Final[str] = Multiline-String
Show Value
"""List candidate indexed terms from one vocabulary.

Use this when you know the vocabulary but want to scan available classes,
properties, individuals, or datatypes before choosing one.

This is especially useful when one vocabulary appears central to the task, has
few enough terms to scan quickly, and may already supply a significant fraction
of your intended graph shape.
Treat this as a bounded familiarization pass to improve modeling decisions, not
as an exhaustive cataloging step.

The vocabulary index is static for this run. You MUST NOT repeat the same
`list_terms` call unchanged and expect a different result. If you still need
different candidates, change `term_type`, `offset`, or `limit`.

Arguments:
- `vocabulary`: vocabulary namespace IRI
- `term_type`: optional filter (`class`, `property`, `individual`, `datatype`)
- `offset`: number of matching terms to skip before returning results
- `limit`: maximum number of terms to return

Example arguments:
- `{"vocabulary": "<http://www.w3.org/2000/01/rdf-schema#>", "term_type": "class", "limit": 25}`
- `{"vocabulary": "<http://www.w3.org/2002/07/owl#>", "term_type": "property", "offset": 10, "limit": 10}`
"""
middleware.rdf_vocabulary_middleware.SEARCH_TERMS_TOOL_DESCRIPTION: Final[str] = Multiline-String
Show Value
"""Search indexed RDF vocabulary terms by intended meaning.

Use this when you know the meaning you want to express but do not yet know the
right indexed term or even the right vocabulary.

This is best for probing overlap between your draft graph shape and the indexed
vocabularies when you can already describe a target concept in words. If one
relevant vocabulary is already known and has few terms, `list_terms` may be
faster and more complete than issuing several narrow searches.
If repeated searches in the same area keep returning plausible candidates from
one small vocabulary, you SHOULD switch to `list_terms` for one bounded scan of
that vocabulary rather than continuing isolated searches.

This search is lexical and deterministic over the static indexed vocabulary set
for this run. You MUST NOT repeat the same `search_terms` call unchanged and
expect a different result. If you still need different candidates, refine the
query, change the vocabulary or term type filters, or inspect one of the
returned candidates.

Arguments:
- `query`: intended meaning, label fragment, or short descriptive phrase
- `vocabularies`: optional indexed vocabulary namespace filter
- `term_types`: optional normalized term type filter (`class`, `property`, `individual`, `datatype`)
- `limit`: maximum number of ranked candidate terms to return

Example arguments:
- `{"query": "birth place", "limit": 8}`
- `{"query": "creator", "vocabularies": ["<http://xmlns.com/foaf/0.1/>"], "term_types": ["property"], "limit": 5}`
"""
middleware.rdf_vocabulary_middleware.INSPECT_TERM_TOOL_DESCRIPTION: Final[str] = Multiline-String
Show Value
"""Inspect one indexed RDF term.

Use this when you already have a candidate term and want to confirm whether it
fits your intended meaning before using it.

By default this returns a compact normalized summary. Set
`include_source_rdf=true` only when you need the original RDF description for
finer detail.

The vocabulary index is static for this run. You MUST NOT repeat the same
`inspect_term` call unchanged and expect new information. After one inspection,
either use the term, inspect a different candidate, or mint a local term if no
indexed term fits.

Treat this as a confirmatory tool for one candidate term or a small number of
modeling-significant terms after familiarization. Do not use repeated
`inspect_term` calls to reconstruct an ontology term by term.

Arguments:
- `term`: indexed term IRI to inspect
- `include_source_rdf`: include Turtle from the source vocabulary when needed

Example arguments:
- `{"term": "<http://www.w3.org/2000/01/rdf-schema#Class>"}`
- `{"term": "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>", "include_source_rdf": true}`
"""
class middleware.rdf_vocabulary_middleware.VocabularySummary(/, **data: Any)

Bases: pydantic.BaseModel

Compact summary of one indexed vocabulary exposed to tools.

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

namespace: axiom.common.N3IRIRef = None
label: str = None
description: str = None
preferredPrefix: str | None = None
preferredNamespace: axiom.common.N3IRIRef | None = None
term_count: pydantic.NonNegativeInt = None
class middleware.rdf_vocabulary_middleware.VocabularyListResponse(/, **data: Any)

Bases: pydantic.BaseModel

Tool response listing indexed vocabularies available in this run.

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

vocabularies: tuple[VocabularySummary, Ellipsis] = None
class middleware.rdf_vocabulary_middleware.ListTermsRequest(/, **data: Any)

Bases: pydantic.BaseModel

Inputs for paginated listing within one indexed vocabulary.

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

vocabulary: axiom.common.N3IRIRef = None
term_type: Literal['class', 'datatype', 'individual', 'property'] | None = None
offset: pydantic.NonNegativeInt = None
limit: pydantic.NonNegativeInt = None
class middleware.rdf_vocabulary_middleware.InspectTermRequest(/, **data: Any)

Bases: pydantic.BaseModel

Inputs for inspecting one indexed vocabulary term.

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

term: axiom.common.N3IRIRef = None
include_source_rdf: bool = None
class middleware.rdf_vocabulary_middleware.TermInspectionResponse(/, **data: Any)

Bases: pydantic.BaseModel

Detailed inspection payload for one indexed vocabulary term.

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

uri: axiom.common.N3IRIRef = None
label: str = None
definition: str = None
termType: middleware.namespaces.common.VocabularyTermType = None
vocabulary: axiom.common.N3IRIRef = None
superTerms: tuple[axiom.common.N3IRIRef, Ellipsis] = None
domain: tuple[axiom.common.N3IRIRef, Ellipsis] = None
range: tuple[axiom.common.N3IRIRef, Ellipsis] = None
source_rdf: middleware.dataset_model.SerializationResponse | None = None
class middleware.rdf_vocabulary_middleware.RDFVocabularyMiddlewareConfig

Configuration for the RDF vocabulary middleware surface.

vocabulary_context: middleware.vocabulary_configuration.VocabularyContext
search_index: middleware.vocabulary.search_index.VocabularySearchIndex | None = None
run_term_telemetry: middleware.shared_services.RunTermTelemetry | None = None
class middleware.rdf_vocabulary_middleware.RDFVocabularyMiddleware(config: RDFVocabularyMiddlewareConfig)

Bases: langchain.agents.middleware.AgentMiddleware[middleware.dataset_state.DatasetState, langchain.agents.middleware.types.ContextT, langchain.agents.middleware.types.ResponseT]

Expose vocabulary tools and guard repeated static lookups via middleware hooks.

vocabulary_context: middleware.vocabulary_configuration.VocabularyContext
tools: collections.abc.Sequence[langchain.tools.BaseTool]

Additional tools registered by the middleware.

run_term_telemetry: middleware.shared_services.RunTermTelemetry
config
property search_index: middleware.vocabulary.search_index.VocabularySearchIndex

Return the configured or lazily built lexical search index.

wrap_model_call(request: langchain.agents.middleware.types.ModelRequest[langchain.agents.middleware.types.ContextT], handler: collections.abc.Callable[[langchain.agents.middleware.types.ModelRequest[langchain.agents.middleware.types.ContextT]], langchain.agents.middleware.types.ModelResponse[langchain.agents.middleware.types.ResponseT]]) → langchain.agents.middleware.types.ModelResponse[langchain.agents.middleware.types.ResponseT]

Append RDF vocabulary guidance to the system prompt before model execution.

async awrap_model_call(request: langchain.agents.middleware.types.ModelRequest[langchain.agents.middleware.types.ContextT], handler: collections.abc.Callable[[langchain.agents.middleware.types.ModelRequest[langchain.agents.middleware.types.ContextT]], collections.abc.Awaitable[langchain.agents.middleware.types.ModelResponse[langchain.agents.middleware.types.ResponseT]]]) → langchain.agents.middleware.types.ModelResponse[langchain.agents.middleware.types.ResponseT]

Append RDF vocabulary guidance to the system prompt before async model execution.

wrap_tool_call(request: langchain.agents.middleware.types.ToolCallRequest, handler: collections.abc.Callable[[langchain.agents.middleware.types.ToolCallRequest], langchain_core.messages.ToolMessage | langgraph.types.Command[Any]]) → langchain_core.messages.ToolMessage | langgraph.types.Command[Any]

Guard repeated static vocabulary lookups and adapt tool-facing errors.

list_vocabularies() → tuple[VocabularySummary, Ellipsis]

Return summaries for every indexed vocabulary visible in this run.

list_terms(vocabulary: rdflib.URIRef | str, term_type: Literal['class', 'datatype', 'individual', 'property'] | None = None, offset: int = 0, limit: int = 50) → tuple[middleware.namespaces.spec_index.VocabularyTerm, Ellipsis]

Return a page of indexed terms from vocabulary.

search_terms(query: str, vocabularies: tuple[rdflib.URIRef | str, Ellipsis] = (), term_types: tuple[middleware.namespaces.common.VocabularyTermType, Ellipsis] = (), limit: int = 8) → middleware.vocabulary.search_model.TermSearchResponse

Rank indexed terms against query using the lexical search index.

inspect_term(term: rdflib.URIRef | str, include_source_rdf: bool = False) → TermInspectionResponse

Return structured metadata and optional source RDF for term.

exception middleware.rdf_vocabulary_middleware.VocabularyNamespaceNotAllowed(namespace: rdflib.URIRef)

Bases: Exception

Raised when a vocabulary namespace is outside the injected whitelist.

namespace
exception middleware.rdf_vocabulary_middleware.IndexedVocabularyNamespaceNotFound(namespace: rdflib.URIRef)

Bases: Exception

Raised when a whitelisted namespace has no indexed vocabulary in this run.

namespace
exception middleware.rdf_vocabulary_middleware.IndexedVocabularyTermNotFound(term: rdflib.URIRef)

Bases: Exception

Raised when a term IRI is not present in the indexed vocabularies.

term
middleware.rdf_vocabulary_middleware.get_transitive_path(graph: rdflib.Graph, leaf_node: rdflib.URIRef, predicate: rdflib.URIRef) → collections.abc.Set[axiom.common.Triple]

Collect transitive predicate edges reachable upward from leaf_node.