I have a custom tool, which creates a python code based on the requirement. I am able to execute the generated python as well. however, next thing I wanted to do is, execute that file automatically. The code works fine if I use the sample coding_task
i copied from website. however, when I use my task, it’s giving error.
however, I am getting following error
Error
I encountered an error while trying to use the tool. This was the error: Arguments validation failed: 1 validation error for CodeInterpreterSchema
libraries_used
Field required [type=missing, input_value={'code': "\nimport pandas...6589', 'metadata': {}}}}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.11/v/missing.
Tool Code Interpreter accepts these inputs: Tool Name: Code Interpreter
Tool Arguments: {'code': {'description': 'Python3 code used to be interpreted in the Docker container. ALWAYS PRINT the final result and the output of the code', 'type': 'str'}, 'libraries_used': {'description': 'List of libraries used in the code with proper installing names separated by commas. Example: numpy,pandas,beautifulsoup4', 'type': 'list[str]'}}
Tool Description: Interprets Python3 code strings with a final print statement..
Moving on then. I MUST either use a tool (use one at time) OR give my best final answer not both at the same time. When responding, I must use the following format:
code
from crewai import Agent, Task, Crew, Process
from crewai_tools import CodeInterpreterTool, FileWriterTool
import os
from datetime import datetime
from salesanalysisagent.tools.schema_validator import SchemaValidatorTool
# Initialize the tool
code_interpreter = CodeInterpreterTool(unsafe_mode=True)
SCRIPT_PATH = os.path.dirname(__file__)
# Define an agent that uses the tool
programmer_agent = Agent(
name="Schema Validator",
role="Python Programmer",
goal="Check schema between source {file_path} and target table and generate an executable python code with proper comments, no need to include any expliantion of code or instruciton on how to execute.",
backstory="This agent ensures data pipelines are consistent by validating schemas.",
tools=[
SchemaValidatorTool(), # Optional if you have one
FileWriterTool(),
CodeInterpreterTool
],
verbose=True,
allow_delegation=False,
)
# Example task to generate and execute code
coding_task = Task(
description="Write a Python function to calculate the Fibonacci sequence up to the 10th number and print the result.",
expected_output="The Fibonacci sequence up to the 10th number.",
agent=programmer_agent,
)
schema_task = Task(
# description= 'based on the differnce in schema and different in data, generate if source has new column add suggest a sql code to add column, if column type or data is mismatched suggest sql code to alter target column type, if not required write a python function using pandas and mysql-connector-python libaray to cast column type to target. make sure during the process there is no data loss or trunction of any kind. Also make sure that no historical data is dropped in a process. code should not have any example, or instruction on how to execute it. Execute the code using CodeInterpreter tool',
description= """
Based on the provided schema difference write a python cod. if source has new columns, suggest a sql code to add columns, if column type or data is mismatched suggest sql code to alter target column type, if not required write a python function using pandas and mysql-connector-python libaray to cast column type to target. make sure during the process there is no data loss or trunction of any kind. Also make sure that no historical data is dropped in a process
""",
expected_output="""
Generate a valid tool call for the CodeInterpreterTool to fix schema mismatch issues.
The output MUST be a JSON object in the following format:
{
"code": "<your executable Python code here>",
"libraries_used": ["pandas", "mysql-connector-python"]
}
Only return this tool call. DO NOT include explanation or Markdown formatting.
""",
# expected_output="data should be updated into target mysql table",
agent=programmer_agent,
)
# Create and run the crew
crew = Crew(
agents=[programmer_agent],
tasks=[schema_task],
verbose=True,
process=Process.sequential,
)
inputs = {
"file_path": os.path.join(
SCRIPT_PATH,
"data",
"self_healing",
"new_data.csv",
), # Replace with actual data file path
"current_year": str(datetime.now().year),
"libraries_used": ["pandas"]
}
result = crew.kickoff(inputs=inputs)