PythonCodeStructuredTool
The PythonCodeStructuredTool component allows you to turn your own Python code into a reusable tool that can be called by other parts of the Nappai dashboard.
You simply paste your dataclass code, give the tool a name and a short description, and the component builds a tool object that the rest of your workflow can use.
How it Works
When you drop this component into a workflow, it reads the information you provide:
- Tool Code – The actual Python dataclass that defines the tool’s logic.
- Tool Name – A short identifier that will appear in the list of available tools.
- Description – A brief explanation of what the tool does, shown to users when they select it.
- Return Directly – A toggle that tells the system whether the tool should return the raw result of its function or wrap it in a standard response format.
- Tool Function – A dropdown that lets you pick which function inside your code should be exposed as the tool’s main action.
- Parent document vectorstore, Classes, Functions – Optional fields that let you link the tool to existing data stores or additional helper classes/functions.
The component then creates two outputs:
- Tool – An object that can be passed to other components that need to call a tool.
- Text – A plain text response that can be displayed directly in the dashboard.
No external APIs are called; everything runs locally inside the Nappai environment.
Inputs
- Tool Code: Enter the dataclass code that implements the tool’s logic.
- Tool Name: Provide a short, unique name for the tool.
- Description: Write a brief description of what the tool does.
- Return Directly: Check this if you want the tool to return the function’s output straight away, without any extra formatting.
- Tool Function: Choose the function from your code that should be exposed as the tool’s main action.
- Parent document vectorstore: (Optional) Connect a vectorstore that the tool can use for document retrieval.
- Classes: (Optional) List any additional classes that the tool needs.
- Functions: (Optional) List any helper functions that the tool should have access to.
Outputs
- Tool: The constructed tool object that can be used by other components.
- Text: A plain‑text response that can be shown in the dashboard or sent to a user.
Usage Example
Suppose you want to create a simple “Add Numbers” tool:
-
Tool Code
from dataclasses import dataclassfrom typing import Any@dataclassclass AddNumbers:def add(self, a: float, b: float) -> float:return a + b -
Tool Name:
AddNumbersTool
-
Description:
Adds two numbers together.
-
Return Directly: Checked
-
Tool Function:
add
Drop the component into your workflow, connect the Tool output to a “Run Tool” component, and you can now call AddNumbersTool
from anywhere in your automation.
Related Components
- CustomPythonCodeStructured – The base component that this tool extends.
- Run Tool – Executes a tool object and returns its result.
- Text Response – Formats plain text for display in the dashboard.
Tips and Best Practices
- Keep your dataclass code simple and well‑documented; this makes it easier to maintain.
- Test your tool locally before adding it to a workflow to catch syntax errors early.
- Use the Return Directly option only if you need the raw output; otherwise, let Nappai handle formatting.
- If your tool needs to access external data, use the Parent document vectorstore field to link it to the appropriate store.
Security Considerations
- The code you paste runs inside Nappai’s sandboxed environment, but it still has access to the data you provide.
- Avoid including sensitive credentials directly in the code; use environment variables or secure secrets instead.
- Review any external libraries you import to ensure they are trusted and up to date.