Handler functions form the core of your Runpod Serverless applications. They define how your process and return results. This section covers everything you need to know about creating effective handler functions.Documentation Index
Fetch the complete documentation index at: https://runpod-b18f5ded-promptless-remove-flash-beta-notification.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Understanding job input
Before writing a handler function, make sure you understand the structure of the input. When your endpoint receives a request, it sends a JSON object to your handler function in this general format:id is a unique identifier for the randomly generated by Runpod, while input contains data sent by the client for your handler function to process.
To learn how to structure requests to your endpoint, see Send API requests.
Basic handler implementation
Here’s a simple handler function that processes an endpoint request:handler.py
runpod.serverless.start() function launches your serverless application with the specified handler.
Local testing
To test your handler locally, you can create atest_input.json file with the input data you want to test:
test_input.json
test_input.json file, you can also provide test input directly in the command line prompt:
Handler types
You can create several types of handler functions depending on the needs of your application.Standard handlers
The simplest handler type, standard handlers process inputs synchronously and return them when the job is complete.handler.py
Streaming handlers
Streaming handlers stream results incrementally as they become available. Use these when your application requires real-time updates, for example when streaming results from a language model.handler.py
/stream operation. Set return_aggregate_stream to True to make outputs available from the /run and /runsync operations as well.
To learn more about aggregating streaming outputs, including best practices for batch processing and handling local testing, see Aggregate streaming outputs.
Asynchronous handlers
Asynchronous handlers process operations concurrently for improved efficiency. Use these for tasks involving I/O operations, API calls, or processing large datasets.handler.py
Concurrent handlers
Concurrent handlers process multiple requests simultaneously with a single worker. Use these for small, rapid operations that don’t fully utlize the worker’s GPU. When increasing concurrency, it’s crucial to monitor memory usage carefully and test thoroughly to determine the optimal concurrency levels for your specific workload. Implement proper error handling to prevent one failing request from affecting others, and continuously monitor and adjust concurrency parameters based on real-world performance. Learn how to build a concurrent handler by following this guide.Error handling
When an exception occurs in your handler function, the Runpod SDK automatically captures it, marks the job status asFAILED and returns the exception details in the job results.
For custom error responses:
handler.py
try/except blocks to avoid unintentionally suppressing errors. Either return the error for a graceful failure or raise it to flag the job as FAILED.
Advanced handler controls
Use these features to fine-tune your Serverless applications for specific use cases.Progress updates
Send progress updates during job execution to inform clients about the current state of processing:handler.py
Worker refresh
For long-running or complex jobs, you may want to refresh the worker after completion to start with a clean state for the next job. Enabling worker refresh clears all logs and wipes the worker state after a job is completed. For example:handler.py
refresh_worker flag. This flag will be removed before the remaining job output is returned.
Handler function best practices
A short list of best practices to keep in mind as you build your handler function:-
Initialize outside the handler: Load models and other heavy resources outside your handler function to avoid repeated initialization.
handler.py
- Input validation: Validate inputs before processing to avoid errors during execution.
- Local testing: Test your handlers locally before deployment.
Payload limits
Be aware of payload size limits when designing your handler:/runoperation: 10 MB/runsyncoperation: 20 MB