Errors, Timeouts, and Untrusted Arguments

Ma Mahalakshmi V Updated 16 Sep 2026
11 min read ·Lesson 35 of 224

Why This Lesson Exists

Every previous lesson in this unit has worked with well-behaved examples: functions that succeed, arguments that arrive in the expected shape, and models that request reasonable things. Production function calling has to handle the cases where none of that holds — a function call that fails partway through, a request that takes too long to complete, and, most importantly, arguments that look syntactically valid but that your code should not blindly trust, because they ultimately originated from a language model's interpretation of a user's request rather than from a source your application fully controls. This lesson treats these as first-class concerns rather than edge cases to patch in later.

The Core Security Principle: Arguments Are Untrusted Input

It's worth stating this plainly and returning to it throughout this lesson: function-call arguments, even though they pass schema validation, are not equivalent to arguments your own code constructed directly. They are the model's interpretation of a user's request, and a user's request can be mistaken, ambiguous, or in a genuinely adversarial case, deliberately crafted to manipulate the model into requesting a function call with harmful arguments (a pattern sometimes called prompt injection when the manipulation comes from content the model reads, such as a document or a web page, rather than the user directly). Schema validation (Lesson 2) constrains the shape of arguments — their types, required fields, allowed values — but it says nothing about whether a schema-valid value is safe or appropriate for your specific function to act on.

# This function call passes schema validation (order_id is a string,
# amount is a number, reason is a string) — but "safe according to the
# schema" and "safe to actually execute" are two different questions.
suspicious_call_arguments = {
    "order_id": "ORD-1",
    "amount": 999999.99,
    "reason": "customer requested",
}

Nothing about this dictionary violates the issue_refund schema from Lesson 4 — every field has the right type, and nothing here is malformed. Whether 999999.99 is a reasonable refund amount for order ORD-1 is a business-logic question the schema cannot answer and was never designed to answer, which is exactly why validation needs to continue inside the function itself, not stop once the schema has passed.

Validating Business Logic Inside the Function

def issue_refund(order_id: str, amount: float, reason: str, order_lookup: dict) -> dict:
    if order_id not in order_lookup:
        return {"success": False, "error": f"Order {order_id} not found."}

    order = order_lookup[order_id]

    if amount > order["total_paid"]:
        return {
            "success": False,
            "error": f"Refund amount {amount} exceeds the order total of {order['total_paid']}.",
        }

    if order["status"] != "delivered":
        return {
            "success": False,
            "error": f"Order {order_id} has status '{order['status']}' and is not eligible for a refund.",
        }

    return {"success": True, "order_id": order_id, "refunded_amount": amount}

orders = {"ORD-1": {"total_paid": 49.99, "status": "delivered"}}
outcome = issue_refund("ORD-1", 999999.99, "customer requested", orders)
print(outcome)  # {'success': False, 'error': 'Refund amount 999999.99 exceeds the order total of 49.99.'}

This mirrors Unit 6's structured-output guidance directly: schema validation and business-logic validation are different checks that serve different purposes, and passing the first does not imply passing the second. Here, checking amount > order["total_paid"] and order["status"] != "delivered" catches exactly the kind of schema-valid-but-substantively-wrong argument the earlier example demonstrated — an amount that is numerically valid but factually incorrect for this specific order, and a status check that prevents a refund on an order that was never actually delivered (perhaps because it was cancelled, or is still in transit). Neither of these checks could have been expressed in the JSON Schema, because they depend on looking up real, current data (order_lookup) that the schema has no access to.

Applying the Principle of Least Privilege

A closely related practice: give each function only the capability it strictly needs, rather than a broad capability that happens to be convenient to implement.

# Overly broad: this function can refund any amount to any order, with no
# built-in ceiling — a bug or a manipulated model could authorize a very
# large, incorrect refund.
def issue_refund_unrestricted(order_id: str, amount: float, reason: str) -> dict:
    process_refund(order_id, amount)  # a stand-in for a real payment API call
    return {"success": True}

# Narrower: caps what a single call can authorize, and requires human
# review above a threshold rather than allowing the model to authorize
# any amount autonomously.
AUTO_APPROVAL_LIMIT = 100.00

def issue_refund_with_limit(order_id: str, amount: float, reason: str, order_lookup: dict) -> dict:
    if amount > AUTO_APPROVAL_LIMIT:
        return {
            "success": False,
            "requires_human_review": True,
            "error": f"Refunds over ${AUTO_APPROVAL_LIMIT} require human approval.",
        }
    # ... the same order_id and status validation as before would go here
    return {"success": True, "order_id": order_id, "refunded_amount": amount}

issue_refund_with_limit() builds in a hard ceiling on what a single, autonomous function call can authorize, routing anything above that ceiling to a requires_human_review outcome rather than an automatic action — a deliberate design choice that limits the blast radius of a single mistaken or manipulated function call, following the same reasoning that leads real payment systems to require human approval above certain thresholds regardless of who or what initiated the request. This is the practical expression of least privilege in a function-calling context: design each function to be capable of exactly what it needs to be capable of, not capable of everything that might theoretically be convenient, especially for functions with real financial, data-modifying, or otherwise consequential side effects.

Handling Timeouts

A function that calls a slow external service (a downstream API, a database under load) needs an explicit timeout, rather than letting a single slow call stall the entire tool-calling loop indefinitely.

import requests

def call_external_service_with_timeout(endpoint: str, payload: dict, timeout_seconds: float = 5.0) -> dict:
    try:
        response = requests.post(endpoint, json=payload, timeout=timeout_seconds)
        response.raise_for_status()
        return {"success": True, "data": response.json()}
    except requests.exceptions.Timeout:
        return {"success": False, "error": f"Request to {endpoint} timed out after {timeout_seconds} seconds."}
    except requests.exceptions.RequestException as e:
        return {"success": False, "error": f"Request to {endpoint} failed: {e}"}

Setting an explicit timeout on the underlying request (rather than relying on whatever default the HTTP library happens to use, which in some libraries is no timeout at all) ensures a single slow or hanging downstream dependency fails predictably and quickly, returning a clear, structured error the calling loop and eventually the model can react to, rather than blocking the entire user-facing interaction for an indefinite period. Catching Timeout specifically alongside the broader RequestException lets the returned error message be more specific and actionable ("timed out" is more informative to a model deciding what to do next than a generic "request failed"), which matters because the model may use this error information to decide whether to retry, try a different approach, or tell the user the service is temporarily unavailable.

Deciding What to Expose in an Error Message

Following Lesson 3's introduction of feeding failures back to the model, it's worth being deliberate about exactly what an error message returned this way contains, since that content may eventually reach an end user through the model's final response.

def query_internal_database_safely(query_params: dict) -> dict:
    try:
        result = run_internal_query(query_params)  # a stand-in for a real database call
        return {"success": True, "data": result}
    except DatabaseConnectionError:
        # Safe: a generic, non-revealing message
        return {"success": False, "error": "The order lookup service is temporarily unavailable. Please try again shortly."}
    except Exception as e:
        # Risky if surfaced directly: str(e) might include a connection string,
        # an internal hostname, a stack trace fragment, or other implementation
        # detail that shouldn't be exposed through a model's response.
        return {"success": False, "error": "An unexpected error occurred while looking up the order."}

The distinction drawn here matters: a specific, safe, actionable message ("temporarily unavailable, try again shortly") is genuinely useful to return, since it lets the model give the user a clear, honest explanation — while a raw exception message from a lower-level library or an internal error is not automatically safe to return verbatim, since it may contain implementation details (a database hostname, an internal file path, a stack trace) that have no business being visible to an end user and were never intended as public-facing text. Deliberately catching specific, anticipated exception types and mapping each to a safe, purpose-written message — rather than passing str(e) straight through for every possible exception — is the practical way to keep this distinction consistent rather than relying on remembering to sanitize every individual error path by hand.

Rate Limiting and Cost Control for Tool-Calling Loops

Since a tool-calling loop can, in principle, run many rounds (bounded only by the max_rounds limit from Lesson 3) and each round involves both a model call and a function execution, it's worth guarding against a loop that technically terminates but still does much more work than a given request actually warrants.

import time

class ToolCallBudget:
    def __init__(self, max_calls: int, max_seconds: float):
        self.max_calls = max_calls
        self.max_seconds = max_seconds
        self.calls_made = 0
        self.start_time = time.monotonic()

    def check_and_increment(self) -> bool:
        elapsed = time.monotonic() - self.start_time
        if self.calls_made >= self.max_calls:
            return False
        if elapsed >= self.max_seconds:
            return False
        self.calls_made += 1
        return True

budget = ToolCallBudget(max_calls=10, max_seconds=30.0)

def dispatch_with_budget(call, available_functions: dict, budget: ToolCallBudget) -> dict:
    if not budget.check_and_increment():
        return {"success": False, "error": "Tool-call budget exceeded for this conversation turn."}
    return dispatch_function_call(call, available_functions)

A ToolCallBudget tracked across an entire user-facing turn (rather than the simpler per-loop max_rounds counter from Lesson 3, which only bounds the number of rounds, not the number of individual calls within a round when multiple tools are requested at once) gives a second, independent layer of protection against a single request consuming disproportionate cost or time — useful in an application where several different tools might be called across several rounds and a hard ceiling on total work per user turn is a reasonable safety and cost-control measure regardless of how the model happens to structure its own calls.

Requiring Explicit Confirmation for Consequential Actions

For a function whose side effects are hard or impossible to undo — issuing a refund, sending an email, deleting a record — a further layer of protection worth building in is requiring an explicit confirmation step between the model deciding to call the function and the function actually executing, rather than executing immediately the moment the model requests it.

PENDING_CONFIRMATION = {}

def request_confirmation(action_id: str, description: str) -> dict:
    PENDING_CONFIRMATION[action_id] = {"description": description, "confirmed": False}
    return {
        "requires_confirmation": True,
        "action_id": action_id,
        "description": description,
        "message": f"This action requires confirmation: {description}",
    }

def confirm_action(action_id: str) -> bool:
    if action_id in PENDING_CONFIRMATION:
        PENDING_CONFIRMATION[action_id]["confirmed"] = True
        return True
    return False

def issue_refund_with_confirmation(order_id: str, amount: float, reason: str, action_id: str) -> dict:
    pending = PENDING_CONFIRMATION.get(action_id)
    if not pending or not pending["confirmed"]:
        return request_confirmation(action_id, f"Refund ${amount} to order {order_id} for: {reason}")
    return {"success": True, "order_id": order_id, "refunded_amount": amount}

The first call to issue_refund_with_confirmation() for a given action_id returns a requires_confirmation result rather than performing the refund — giving the calling application (and, through it, an actual human, whether that's the end user or a support agent operating the tool) a chance to review the specific action described before it happens, with the refund only actually executing once confirm_action() has been called through a separate, deliberate step outside the model's own function-calling flow. This pattern is worth reserving for genuinely consequential, hard-to-reverse actions rather than applying it to every function indiscriminately — a read-only lookup gains nothing from a confirmation step and just adds friction, while an irreversible financial or destructive action benefits meaningfully from a human getting an explicit, final say before it actually happens.

Common Mistakes

Treating schema-valid arguments as automatically safe or correct, skipping business-logic validation inside the function itself and trusting the schema to have already caught every problem it cannot actually catch.

Giving a function broader capability than it strictly needs (an unrestricted refund amount, an unrestricted database query) rather than building in explicit limits and requiring human review above a defined threshold.

Omitting an explicit timeout on calls to external services, allowing a single slow dependency to stall an entire tool-calling interaction indefinitely.

Returning raw exception messages (str(e)) directly as function results, potentially exposing internal implementation details to an end user through the model's eventual response.

Best Practices

Validate business logic inside every function, independent of schema validation, treating "the arguments matched the schema" and "the arguments are safe and correct for this specific call" as two separate questions.

Apply the principle of least privilege to every tool with real side effects, building in explicit limits and routing anything beyond those limits to human review rather than full autonomous execution.

Set explicit, reasonable timeouts on every call to an external service, and return a clear, structured error when a timeout occurs rather than letting the failure propagate unpredictably.

Curate error messages deliberately by catching specific, anticipated exception types and mapping each to a safe, purpose-written message, rather than passing raw exception text through to a result the model (and potentially an end user) will see.

Track a cumulative call budget across an entire user-facing turn, not just a per-round limit, to bound total cost and time regardless of how many tools get called across however many rounds.

0 Comments

Reviewed before they appear

No comments yet.

OpenAI SDK
Introduction to the OpenAI SDK Setting Up Python Creating an API Key Your First Call — client.responses.create() and response.output_text Understanding Billing, Credits, and What a Request Costs Why Responses Replaced Chat Completions Anatomy of a Request: model, input, and instructions Anatomy of a Response: The Typed output Array, Not Just Text Roles: User, Assistant, and Developer/System Choosing a Model, and Reading the Models Page Instead of Memorizing Names Instructions vs. Input Writing Prompts That Get Consistent Results Few-Shot Examples Reasoning Models and the reasoning Parameter Debugging a Prompt That Misbehaves Why Streaming Matters for User Experience stream=True and Iterating Over Events Handling the Event Types You Actually Care About Background Mode for Long-Running Jobs Project — Add Live Streaming to Your Chatbot The Problem With Parsing Free Text JSON Schema and Strict Mode Pydantic Models With the SDK's Parse Helpers Handling Refusals and Validation Failures Project — A Resume-to-JSON Extractor Working With input_image input_file, PDFs, and the Files API Image Generation Speech-to-Text and Text-to-Speech Project: A PDF Question-Answering Script What Function Calling Is Defining a Tool Schema The Full Loop Multiple Tools Errors, Timeouts, and Untrusted Arguments Project: A Weather Assistant Web Search File Search and Vector Stores Code Interpreter Remote MCP Servers and Connectors Project: A Research Assistant What an Embedding Is, Without the Maths Generating and Storing Embeddings Similarity Search From Scratch Hosted Vector Stores vs. Rolling Your Own A Small RAG App Over a Folder of Notes Agents vs. a Single API Call — When You Need One pip install openai Giving Agents Tools Handoffs and Multi-Agent Triage Guardrails and Approvals Tracing and Observing What Your Agent Did A Multi-Agent Support Desk Error Codes and What Each One Means Retries, Timeouts, and Backoff Rate Limits and Spend Limits Prompt Caching and Cost Optimisation The Batch API for Bulk Work Async Clients and Concurrency Moderation and Safety Best Practices Designing the App Backend With FastAPI Streaming to a Simple Frontend Deploying and a Cost/Safety Checklist Why Web Search Is Useful for Current Information Using the Web Search Tool with the Responses API Configuring Search Behavior for Application Use Cases Understanding Citations and Source Attribution Where to Go Next Building a Research Assistant with Web Search Combining Web Search with Structured Outputs Handling Conflicting or Low-Quality Web Sources Reducing Unsupported Claims with Grounded Generation Testing Freshness-Sensitive AI Answers Production Considerations for Web-Grounded Applications Understanding File Search and Retrieval-Augmented Generation Creating and Organizing Vector Stores Uploading Documents for Retrieval Connecting Vector Stores to Responses API Requests Designing Document Metadata and Filtering Strategies Building a PDF Question-Answering Application Improving Retrieval Quality With Better Document Preparation Handling Missing Evidence and Retrieval Failures Combining File Search With Web Search Building a Production Knowledge-Base Assistant What the Code Interpreter Tool Is Designed For Running Python-Based Analysis Through the OpenAI SDK Uploading Datasets for Analysis Analyzing CSV and Spreadsheet Data Generating Charts and Data Summaries Handling Generated Files and Downloadable Artifacts Building a Data-Analysis Assistant Combining Code Execution with Structured Outputs Validating Generated Calculations and Results Security and Sandbox Considerations for Code Execution Understanding Multimodal Input with the OpenAI SDK Sending Images to a Model Image Analysis from URLs and Uploaded Files Extracting Text and Information from Screenshots Building an Image-Question-Answering Application Combining Image Input with Structured Output Analyzing Multiple Images in One Request Handling Image Quality and Input Limitations Designing Multimodal Prompts for Reliable Results Building a Practical Vision-Powered Python Application Understanding Speech-to-Text and Text-to-Speech Workflows Transcribing Audio with the OpenAI SDK Working with Uploaded Audio Files Handling Timestamps and Transcription Metadata Building a Meeting Transcription Workflow Generating Spoken Responses from Text Handling Long Audio and Processing Failures Combining Audio with Text and Tool Calling Building an End-to-End Python Voice Application What Embeddings Are and When to Use Them Generating Embeddings With the OpenAI API Preparing Text for Embedding Comparing Vectors With Cosine Similarity Building a Simple Semantic Search Engine in Python Storing Embeddings in a Database Metadata Filtering for Semantic Search Chunking Strategies for Better Retrieval Evaluating Semantic Search Quality Building a Document Similarity Application When Batch Processing Makes Sense Designing Large-Volume AI Processing Pipelines Using Asynchronous Python with the OpenAI SDK Running Concurrent Requests Safely Controlling Concurrency and Avoiding Rate Limits Tracking Batch Job Progress Handling Partial Failures in Bulk Workloads Retrying Failed Items Without Duplicating Successful Work Designing Resumable AI Processing Jobs Building a Production Batch-Processing Pipeline Batch Processing Makes Sense Large-Scale AI Processing Pipelines Async Python with OpenAI SDK Safe Concurrent Requests Concurrency & Rate Limits Batch Progress Tracking Partial Failure Handling Safe Retry Handling Resumable AI Jobs Production Batch Pipeline System–User Data Separation Reusable App Instructions Prompt Templates & Variables Extraction & Classification Prompts Summarization & Transformation Prompts Explicit Output Requirements Prompt Version Management Prompt Testing & Evaluation Reusable Python Prompt Library API Key Security Secure API Key Storage Secure Secret Management Prompt Injection Prevention Trusted vs. Untrusted Content Tool Argument Validation Sensitive Data Handling Secure Logging AI Action Authorization Production AI Security Checklist Why AI Applications Need Evaluation Beyond Unit Tests Unit Testing OpenAI SDK Integration Code Mocking API Responses in Python Tests Testing Structured Outputs Against Schemas Testing Tool-Calling Workflows Building a Small Evaluation Dataset Measuring Accuracy, Consistency, and Failure Rates Regression Testing Prompts and Model Changes Human Evaluation Versus Automated Evaluation Creating a Repeatable Evaluation Pipeline AI Request Monitoring Token Cost Management Usage Metrics Design Reducing Model Calls Prompt & Context Optimization Model Selection & Optimization AI Caching Strategies Interactive Latency Optimization Usage Dashboards & Budget Alerts Performance & Cost Checklist Every API Call Starts Fresh Fixing API Statelessness Server-Side Conversation Memory Limits of Response Chaining What We're Building Conversation Memory Challenges Preparing an OpenAI SDK Application for Deployment Environment-Specific Configuration for Development and Production Deploying a Python AI Service with Docker Container Health Checks and Startup Configuration Managing Secrets in Cloud Deployments Background Workers for Long-Running AI Tasks Queues and Asynchronous Job Architectures Scaling AI Workloads Horizontally Monitoring Production Incidents and Failures Production Deployment Checklist for OpenAI SDK Applications Reusable OpenAI Service Classes AI Client Dependency Injection Typed AI Responses Python Configuration Management AI Request Decorators Centralized AI Error Handling Clean SDK Abstractions Reusable OpenAI Utilities Internal AI Python Libraries SDK Integration Maintenance Production AI Chatbot Document Q&A System Web Research Assistant Customer Support Agent AI Data Analysis Assistant Image Analysis App Meeting Transcription & Summary Semantic Document Search Multi-Tool AI Agent Production OpenAI SDK App Why "It Looked Fine When I Tested It" Isn't Enough Timing Note Status Note Pre-Decision Status Note Current Availability Note
Ask about this post
AI Ask about this post

Ask questions about Errors, Timeouts, and Untrusted Arguments and get answers drawn from it.

Signed-in readers only.