Skip to content

LLM context node

kedro.pipeline.llm_context

Experimental utilities for constructing an LLMContext inside a Kedro pipeline.

This module provides:

  • A tool() builder for declaring tool constructors and their Kedro inputs.
  • An llm_context_node() helper that creates a Kedro node which assembles:
  • an LLM instance,
  • prompt datasets,
  • dynamically built tool objects.

All datasets required by the context (LLM, prompts, tool inputs) are validated and loaded by Kedro before the node runs. Tools are instantiated at execution time and automatically assigned readable names based on the returned objects.

LLMContext dataclass

LLMContext(context_id, llm, prompts=dict(), tools=dict())

Experimental

This API is experimental and may change in future Kedro releases.

Runtime context passed to an LLM execution step.

Parameters:

  • context_id (str) –

    Logical identifier for the context (usually the node name).

  • llm (object) –

    The LLM or LLM wrapper loaded by Kedro.

  • prompts (dict[str, Any], default: dict() ) –

    A mapping of prompt dataset names → prompt content.

  • tools (dict[str, object], default: dict() ) –

    A mapping of tool names to instantiated tool objects. Tool names are automatically derived from each built tool object.

LLMContextNode

LLMContextNode(*, outputs, llm, prompts, tools=None, name=None, tags=None, confirms=None, namespace=None, preview_fn=None)

Bases: Node

Experimental

This API is experimental and may change in future Kedro releases.

A Kedro Node that constructs an LLMContext at execution time.

This node assembles a runtime context consisting of:

  • an LLM instance loaded by Kedro,
  • one or more prompt datasets,
  • optionally, dynamically constructed tool objects.

All required datasets (LLM, prompts, tool inputs) are validated and loaded by Kedro before execution. Tools are instantiated during node execution and assigned readable names derived from the returned tool objects.

Example:

LLMContextNode(
    name="response_agent_context_node",
    outputs="response_generation_context",
    llm="llm",
    prompts=["tool_prompt", "response_prompt"],
    tools=[
        tool(build_get_user_claims, "db_engine"),
        tool(build_lookup_docs, "docs", "params:docs_matches"),
        tool(build_create_claim, "db_engine"),
    ],
)

Parameters:

  • outputs (str) –

    Name of the output dataset that will receive the LLMContext.

  • llm (str) –

    Name of the Kedro dataset containing the LLM instance.

  • prompts (list[str]) –

    List of dataset names containing prompt content.

  • tools (list[_ToolConfig] | None, default: None ) –

    Optional list of tool configurations created via tool(...).

  • name (str | None, default: None ) –

    Optional node name; also used as the logical context identifier.

  • tags (str | Iterable[str] | None, default: None ) –

    Optional set of tags to be applied to the node.

  • confirms (str | list[str] | None, default: None ) –

    Optional name or the list of the names of the datasets that should be confirmed.

  • namespace (str | None, default: None ) –

    Optional node namespace.

  • preview_fn (Callable[..., PreviewPayload] | None, default: None ) –

    Optional preview function that returns one of the valid preview types (TextPreview, MermaidPreview, ImagePreview, or CustomPreview). This is an experimental feature.

Source code in kedro/pipeline/llm_context.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
def __init__(  # noqa: PLR0913
    self,
    *,
    outputs: str,
    llm: str,
    prompts: list[str],
    tools: list[_ToolConfig] | None = None,
    name: str | None = None,
    tags: str | Iterable[str] | None = None,
    confirms: str | list[str] | None = None,
    namespace: str | None = None,
    preview_fn: Callable[..., PreviewPayload] | None = None,
):
    """Create an LLMContextNode.

    Args:
        outputs: Name of the output dataset that will receive the `LLMContext`.
        llm: Name of the Kedro dataset containing the LLM instance.
        prompts: List of dataset names containing prompt content.
        tools: Optional list of tool configurations created via `tool(...)`.
        name: Optional node name; also used as the logical context identifier.
        tags: Optional set of tags to be applied to the node.
        confirms: Optional name or the list of the names of the datasets
            that should be confirmed.
        namespace: Optional node namespace.
        preview_fn: Optional preview function that returns one of the valid
            preview types (TextPreview, MermaidPreview, ImagePreview, or CustomPreview).
            This is an experimental feature.
    """
    inputs = {"llm": llm}

    # Add prompts as validated inputs
    for p in prompts:
        inputs[p] = p

    # Add tool inputs (datasets + params)
    if tools:
        for t in tools:
            for inp in t.inputs:
                inputs[inp] = inp

    def construct_context(llm: object, **kwargs: dict[str, Any]) -> LLMContext:
        """Node execution function.

        Collects prompt values, instantiates tools using their declared
        inputs, and returns an `LLMContext`.
        """
        # Collect prompts
        prompts_dict = {p: kwargs[p] for p in prompts}

        # Build tools
        built_tools = {}
        if tools:
            for t in tools:
                tool_inputs = {
                    inp.replace("params:", ""): kwargs[inp] for inp in t.inputs
                }
                built_tool = t.func(**tool_inputs)
                built_tools[_get_tool_name(built_tool)] = built_tool

        return LLMContext(
            context_id=name or f"llm_context_node__{_normalize_outputs(outputs)}",
            llm=llm,
            prompts=prompts_dict,
            tools=built_tools,
        )

    # call the Node constructor with required arguments
    super().__init__(
        func=construct_context,
        inputs=inputs,
        outputs=outputs,
        name=name,
        tags=tags,
        confirms=confirms,
        namespace=namespace,
        preview_fn=preview_fn,
    )

llm_context_node

llm_context_node(*, outputs, llm, prompts, tools=None, name=None, tags=None, confirms=None, namespace=None, preview_fn=None)

Experimental

This API is experimental and may change in future Kedro releases.

Create a Kedro node that builds an LLMContext at runtime.

This is a convenience wrapper around LLMContextNode that mirrors the standard functional node construction style used in Kedro pipelines.

Parameters:

  • outputs (str) –

    Name of the output dataset that will receive the LLMContext.

  • llm (str) –

    Name of the Kedro dataset containing the LLM instance.

  • prompts (list[str]) –

    List of dataset names containing prompt content.

  • tools (list[_ToolConfig] | None, default: None ) –

    Optional list of tool configurations created via tool(...). Each tool declares the Kedro inputs required to construct it.

  • name (str | None, default: None ) –

    Optional node name; also used as the logical context identifier.

  • tags (str | Iterable[str] | None, default: None ) –

    Optional set of tags to be applied to the node.

  • confirms (str | list[str] | None, default: None ) –

    Optional name or the list of the names of the datasets that should be confirmed.

  • namespace (str | None, default: None ) –

    Optional node namespace.

  • preview_fn (Callable[..., PreviewPayload] | None, default: None ) –

    Optional preview function that returns one of the valid preview types (TextPreview, MermaidPreview, ImagePreview, or CustomPreview). This is an experimental feature.

Returns:

  • Node

    A Kedro Node that loads all declared datasets, instantiates tools,

  • Node

    collects prompt values, and returns an LLMContext.

Example:

llm_context_node(
    name="response_agent_context_node",
    outputs="response_generation_context",
    llm="llm",
    prompts=["tool_prompt", "response_prompt"],
    tools=[
        tool(build_get_user_claims, "db_engine"),
        tool(build_lookup_docs, "docs", "params:docs_matches"),
        tool(build_create_claim, "db_engine"),
    ],
)

Source code in kedro/pipeline/llm_context.py
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
@experimental
def llm_context_node(  # noqa: PLR0913
    *,
    outputs: str,
    llm: str,
    prompts: list[str],
    tools: list[_ToolConfig] | None = None,
    name: str | None = None,
    tags: str | Iterable[str] | None = None,
    confirms: str | list[str] | None = None,
    namespace: str | None = None,
    preview_fn: Callable[..., PreviewPayload] | None = None,
) -> Node:
    """
    !!! warning "Experimental"
        This API is experimental and may change in future Kedro releases.

    Create a Kedro node that builds an `LLMContext` at runtime.

    This is a convenience wrapper around `LLMContextNode` that mirrors
    the standard functional node construction style used in Kedro pipelines.

    Args:
        outputs: Name of the output dataset that will receive the `LLMContext`.
        llm: Name of the Kedro dataset containing the LLM instance.
        prompts: List of dataset names containing prompt content.
        tools: Optional list of tool configurations created via `tool(...)`.
            Each tool declares the Kedro inputs required to construct it.
        name: Optional node name; also used as the logical context identifier.
        tags: Optional set of tags to be applied to the node.
        confirms: Optional name or the list of the names of the datasets
            that should be confirmed.
        namespace: Optional node namespace.
        preview_fn: Optional preview function that returns one of the valid
            preview types (TextPreview, MermaidPreview, ImagePreview, or CustomPreview).
            This is an experimental feature.

    Returns:
        A Kedro Node that loads all declared datasets, instantiates tools,
        collects prompt values, and returns an `LLMContext`.

    Example:
    ```python
    llm_context_node(
        name="response_agent_context_node",
        outputs="response_generation_context",
        llm="llm",
        prompts=["tool_prompt", "response_prompt"],
        tools=[
            tool(build_get_user_claims, "db_engine"),
            tool(build_lookup_docs, "docs", "params:docs_matches"),
            tool(build_create_claim, "db_engine"),
        ],
    )
    ```
    """
    return LLMContextNode(
        outputs=outputs,
        llm=llm,
        prompts=prompts,
        tools=tools,
        name=name,
        tags=tags,
        confirms=confirms,
        namespace=namespace,
        preview_fn=preview_fn,
    )

tool

tool(func, *inputs)

Experimental

This API is experimental and may change in future Kedro releases.

Create a _ToolConfig definition for a tool builder.

Parameters:

  • func (Callable[..., T]) –

    A callable that constructs and returns a tool object. Its name will not define the tool name; the name is derived from the returned object when the tool is instantiated.

  • *inputs (str, default: () ) –

    Kedro dataset names (including params:* entries) required by func.

Returns:

  • _ToolConfig

    A configuration object used by llm_context_node to construct tools

  • _ToolConfig

    at execution time

Source code in kedro/pipeline/llm_context.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
@experimental
def tool(func: Callable[..., T], *inputs: str) -> _ToolConfig:
    """
    !!! warning "Experimental"
        This API is experimental and may change in future Kedro releases.

    Create a `_ToolConfig` definition for a tool builder.

    Args:
        func: A callable that constructs and returns a tool object.
            Its name will *not* define the tool name; the name is derived from
            the returned object when the tool is instantiated.
        *inputs: Kedro dataset names (including params:* entries) required by `func`.

    Returns:
        A configuration object used by ``llm_context_node`` to construct tools
        at execution time
    """
    return _ToolConfig(func=func, inputs=list(inputs))