middleware.rdf_vocabulary_middleware
====================================
.. py:module:: middleware.rdf_vocabulary_middleware
.. autoapi-nested-parse::
LangChain middleware and tools for search-first RDF vocabulary retrieval.
Attributes
----------
.. autoapisummary::
middleware.rdf_vocabulary_middleware.logger
middleware.rdf_vocabulary_middleware.LIST_VOCABULARIES_TOOL_DESCRIPTION
middleware.rdf_vocabulary_middleware.LIST_TERMS_TOOL_DESCRIPTION
middleware.rdf_vocabulary_middleware.SEARCH_TERMS_TOOL_DESCRIPTION
middleware.rdf_vocabulary_middleware.INSPECT_TERM_TOOL_DESCRIPTION
Exceptions
----------
.. autoapisummary::
middleware.rdf_vocabulary_middleware.VocabularyNamespaceNotAllowed
middleware.rdf_vocabulary_middleware.IndexedVocabularyNamespaceNotFound
middleware.rdf_vocabulary_middleware.IndexedVocabularyTermNotFound
Classes
-------
.. autoapisummary::
middleware.rdf_vocabulary_middleware.VocabularySummary
middleware.rdf_vocabulary_middleware.VocabularyListResponse
middleware.rdf_vocabulary_middleware.ListTermsRequest
middleware.rdf_vocabulary_middleware.InspectTermRequest
middleware.rdf_vocabulary_middleware.TermInspectionResponse
middleware.rdf_vocabulary_middleware.RDFVocabularyMiddlewareConfig
middleware.rdf_vocabulary_middleware.RDFVocabularyMiddleware
Functions
---------
.. autoapisummary::
middleware.rdf_vocabulary_middleware.get_transitive_path
Module Contents
---------------
.. py:data:: logger
.. py:data:: LIST_VOCABULARIES_TOOL_DESCRIPTION
:type: Final[str]
:value: Multiline-String
.. raw:: html
Show Value
.. code-block:: python
"""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.
"""
.. raw:: html
.. py:data:: LIST_TERMS_TOOL_DESCRIPTION
:type: Final[str]
:value: Multiline-String
.. raw:: html
Show Value
.. code-block:: python
"""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": "", "term_type": "class", "limit": 25}`
- `{"vocabulary": "", "term_type": "property", "offset": 10, "limit": 10}`
"""
.. raw:: html
.. py:data:: SEARCH_TERMS_TOOL_DESCRIPTION
:type: Final[str]
:value: Multiline-String
.. raw:: html
Show Value
.. code-block:: python
"""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": [""], "term_types": ["property"], "limit": 5}`
"""
.. raw:: html
.. py:data:: INSPECT_TERM_TOOL_DESCRIPTION
:type: Final[str]
:value: Multiline-String
.. raw:: html
Show Value
.. code-block:: python
"""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": ""}`
- `{"term": "", "include_source_rdf": true}`
"""
.. raw:: html
.. py:class:: VocabularySummary(/, **data: Any)
Bases: :py:obj:`pydantic.BaseModel`
Compact summary of one indexed vocabulary exposed to tools.
.. py:attribute:: model_config
Configuration for the model, should be a dictionary conforming to [`ConfigDict`][pydantic.config.ConfigDict].
.. py:attribute:: namespace
:type: rdflib_reasoning.axiom.common.N3IRIRef
:value: None
.. py:attribute:: label
:type: str
:value: None
.. py:attribute:: description
:type: str
:value: None
.. py:attribute:: preferredPrefix
:type: str | None
:value: None
.. py:attribute:: preferredNamespace
:type: rdflib_reasoning.axiom.common.N3IRIRef | None
:value: None
.. py:attribute:: term_count
:type: pydantic.NonNegativeInt
:value: None
.. py:class:: VocabularyListResponse(/, **data: Any)
Bases: :py:obj:`pydantic.BaseModel`
Tool response listing indexed vocabularies available in this run.
.. py:attribute:: model_config
Configuration for the model, should be a dictionary conforming to [`ConfigDict`][pydantic.config.ConfigDict].
.. py:attribute:: vocabularies
:type: tuple[VocabularySummary, Ellipsis]
:value: None
.. py:class:: ListTermsRequest(/, **data: Any)
Bases: :py:obj:`pydantic.BaseModel`
Inputs for paginated listing within one indexed vocabulary.
.. py:attribute:: model_config
Configuration for the model, should be a dictionary conforming to [`ConfigDict`][pydantic.config.ConfigDict].
.. py:attribute:: vocabulary
:type: rdflib_reasoning.axiom.common.N3IRIRef
:value: None
.. py:attribute:: term_type
:type: Literal['class', 'datatype', 'individual', 'property'] | None
:value: None
.. py:attribute:: offset
:type: pydantic.NonNegativeInt
:value: None
.. py:attribute:: limit
:type: pydantic.NonNegativeInt
:value: None
.. py:class:: InspectTermRequest(/, **data: Any)
Bases: :py:obj:`pydantic.BaseModel`
Inputs for inspecting one indexed vocabulary term.
.. py:attribute:: model_config
Configuration for the model, should be a dictionary conforming to [`ConfigDict`][pydantic.config.ConfigDict].
.. py:attribute:: term
:type: rdflib_reasoning.axiom.common.N3IRIRef
:value: None
.. py:attribute:: include_source_rdf
:type: bool
:value: None
.. py:class:: TermInspectionResponse(/, **data: Any)
Bases: :py:obj:`pydantic.BaseModel`
Detailed inspection payload for one indexed vocabulary term.
.. py:attribute:: model_config
Configuration for the model, should be a dictionary conforming to [`ConfigDict`][pydantic.config.ConfigDict].
.. py:attribute:: uri
:type: rdflib_reasoning.axiom.common.N3IRIRef
:value: None
.. py:attribute:: label
:type: str
:value: None
.. py:attribute:: definition
:type: str
:value: None
.. py:attribute:: termType
:type: rdflib_reasoning.middleware.namespaces.common.VocabularyTermType
:value: None
.. py:attribute:: vocabulary
:type: rdflib_reasoning.axiom.common.N3IRIRef
:value: None
.. py:attribute:: superTerms
:type: tuple[rdflib_reasoning.axiom.common.N3IRIRef, Ellipsis]
:value: None
.. py:attribute:: domain
:type: tuple[rdflib_reasoning.axiom.common.N3IRIRef, Ellipsis]
:value: None
.. py:attribute:: range
:type: tuple[rdflib_reasoning.axiom.common.N3IRIRef, Ellipsis]
:value: None
.. py:attribute:: source_rdf
:type: rdflib_reasoning.middleware.dataset_model.SerializationResponse | None
:value: None
.. py:class:: RDFVocabularyMiddlewareConfig
Configuration for the RDF vocabulary middleware surface.
.. py:attribute:: vocabulary_context
:type: rdflib_reasoning.middleware.vocabulary_configuration.VocabularyContext
.. py:attribute:: search_index
:type: rdflib_reasoning.middleware.vocabulary.search_index.VocabularySearchIndex | None
:value: None
.. py:attribute:: run_term_telemetry
:type: rdflib_reasoning.middleware.shared_services.RunTermTelemetry | None
:value: None
.. py:class:: RDFVocabularyMiddleware(config: RDFVocabularyMiddlewareConfig)
Bases: :py:obj:`langchain.agents.middleware.AgentMiddleware`\ [\ :py:obj:`rdflib_reasoning.middleware.dataset_state.DatasetState`\ , :py:obj:`langchain.agents.middleware.types.ContextT`\ , :py:obj:`langchain.agents.middleware.types.ResponseT`\ ]
Expose vocabulary tools and guard repeated static lookups via middleware hooks.
.. py:attribute:: vocabulary_context
:type: rdflib_reasoning.middleware.vocabulary_configuration.VocabularyContext
.. py:attribute:: tools
:type: collections.abc.Sequence[langchain.tools.BaseTool]
Additional tools registered by the middleware.
.. py:attribute:: run_term_telemetry
:type: rdflib_reasoning.middleware.shared_services.RunTermTelemetry
.. py:attribute:: config
.. py:property:: search_index
:type: rdflib_reasoning.middleware.vocabulary.search_index.VocabularySearchIndex
Return the configured or lazily built lexical search index.
.. py:method:: 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.
.. py:method:: 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]
:async:
Append RDF vocabulary guidance to the system prompt before async model execution.
.. py:method:: 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.
.. py:method:: list_vocabularies() -> tuple[VocabularySummary, Ellipsis]
Return summaries for every indexed vocabulary visible in this run.
.. py:method:: list_terms(vocabulary: rdflib.URIRef | str, term_type: Literal['class', 'datatype', 'individual', 'property'] | None = None, offset: int = 0, limit: int = 50) -> tuple[rdflib_reasoning.middleware.namespaces.spec_index.VocabularyTerm, Ellipsis]
Return a page of indexed terms from ``vocabulary``.
.. py:method:: search_terms(query: str, vocabularies: tuple[rdflib.URIRef | str, Ellipsis] = (), term_types: tuple[rdflib_reasoning.middleware.namespaces.common.VocabularyTermType, Ellipsis] = (), limit: int = 8) -> rdflib_reasoning.middleware.vocabulary.search_model.TermSearchResponse
Rank indexed terms against ``query`` using the lexical search index.
.. py:method:: inspect_term(term: rdflib.URIRef | str, include_source_rdf: bool = False) -> TermInspectionResponse
Return structured metadata and optional source RDF for ``term``.
.. py:exception:: VocabularyNamespaceNotAllowed(namespace: rdflib.URIRef)
Bases: :py:obj:`Exception`
Raised when a vocabulary namespace is outside the injected whitelist.
.. py:attribute:: namespace
.. py:exception:: IndexedVocabularyNamespaceNotFound(namespace: rdflib.URIRef)
Bases: :py:obj:`Exception`
Raised when a whitelisted namespace has no indexed vocabulary in this run.
.. py:attribute:: namespace
.. py:exception:: IndexedVocabularyTermNotFound(term: rdflib.URIRef)
Bases: :py:obj:`Exception`
Raised when a term IRI is not present in the indexed vocabularies.
.. py:attribute:: term
.. py:function:: get_transitive_path(graph: rdflib.Graph, leaf_node: rdflib.URIRef, predicate: rdflib.URIRef) -> collections.abc.Set[rdflib_reasoning.axiom.common.Triple]
Collect transitive ``predicate`` edges reachable upward from ``leaf_node``.