Agent Foundry
All Problems

#12. Tool Parameter Validator

MediumTool CallingGuardrails

The Problem

Your business assistant has three tools — create_invoice, schedule_meeting, and send_notification — but none of them validate their inputs. When the model passes a string like "150" instead of the integer 150 for the invoice amount, the tool crashes with a TypeError. Missing required fields cause NoneType errors. There are no type hints, no validation, and no helpful error messages — just raw Python tracebacks that confuse both the agent and the user. Your job is to add proper type hints, input validation, and helpful error messages so the tools reject bad input gracefully and work correctly with valid input.

Examples

Example 1

User input: Create an invoice for John Doe for 150 dollars with 3 items

Current (bad) output: TypeError: can't multiply sequence by non-int of type 'float' (because amount came in as a string)

Expected (good) output: Invoice created successfully — "Invoice for John Doe: 3 items, total $165.00"

Example 2

User input: Schedule a meeting called "Sprint Review" for sixty minutes with Alice and Bob

Current (bad) output: TypeError: unsupported operand type(s) for +: 'str' and 'int' (because duration_minutes came in as a string)

Expected (good) output: Either the tool coerces "sixty" to 60, or returns: "Error: duration_minutes must be a number (e.g., 60), got 'sixty'."

Example 3

User input: Send a notification to user 42 with high priority

Current (bad) output: TypeError: '>' not supported between instances of 'str' and 'int' (because priority came as a string)

Expected (good) output: "Error: priority must be a number between 1 and 5, got 'high'. Please provide a numeric priority."

Your Task

Fix all three tool functions so that they:

  • Have proper type hints for every parameter (e.g., amount: float, duration_minutes: int).
  • Validate that each parameter is the correct type and within a valid range.
  • Return a helpful error message when validation fails, explaining what was expected.
  • Continue to work correctly when given properly typed, valid input.

Do not change the agent setup, prompt, or model configuration.

Evaluation

Submissions are checked for the following:

  • Validates parameter types: Each tool checks that inputs are the correct type before processing.
  • Rejects invalid input: Bad parameters produce clear error messages, not crashes or tracebacks.
  • Provides helpful error messages: Errors explain what was wrong and what valid input looks like.
  • Valid input still works: The tools produce correct results when given properly typed parameters.

Constraints

  • You must fix the tool schemas and add validation logic
  • Error messages must be user-friendly, not raw tracebacks
  • The tools must still work correctly with valid input
Starter Code
from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.tools import tool

llm = ChatOpenAI(model="gpt-4o-mini")

# BUG: These tools accept any input without validation — wrong types cause crashes
@tool
def create_invoice(customer_name, amount, items):
    """Create an invoice for a customer."""
    total = amount * 1.1  # crashes if amount is a string
    return f"Invoice for {customer_name}: {items} items, total ${total:.2f}"

@tool
def schedule_meeting(title, duration_minutes, attendees):
    """Schedule a meeting."""
    end_time = duration_minutes + 30  # crashes if duration is a string
    return f"Meeting '{title}' scheduled for {duration_minutes}min with {len(attendees)} attendees"

@tool
def send_notification(user_id, message, priority):
    """Send a notification to a user."""
    if priority > 5:  # crashes if priority is a string
        return "Priority too high"
    return f"Notification sent to user {user_id}: {message} (priority: {priority})"

tools = [create_invoice, schedule_meeting, send_notification]

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a business assistant. Use tools to help with invoices, meetings, and notifications."),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])

agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools)

result = executor.invoke({"input": "Create an invoice for John Doe for 150 dollars with 3 items"})
print(result["output"])
Open in Google Colab
Evaluation Criteria0/4