AgentsAgentBase

handle_serverless_request

View as MarkdownOpen in Claude

Handle incoming requests in serverless environments. This method dispatches to the appropriate handler based on the detected (or forced) execution mode: CGI, Lambda, Google Cloud Functions, or Azure Functions.

In most cases, use run() instead, which auto-detects the environment and calls this method when appropriate. Call handle_serverless_request() directly only when you need explicit control over the serverless dispatch.

The method automatically:

  • Parses the request body for SWAIG function calls
  • Routes to the correct SWAIG function handler
  • Returns the SWML document for root path requests
  • Validates authentication credentials

Supported platforms: AWS Lambda (HTTP API v1 and v2), Google Cloud Functions (Flask), Azure Functions (HttpRequest/HttpResponse), and CGI.

Parameters

event
Optional[Any]

Platform-specific event or request object:

  • Lambda: The event dictionary from the Lambda handler
  • Google Cloud Functions: The Flask request object
  • Azure Functions: The HttpRequest object
  • CGI: Not used (reads from stdin and environment variables)
context
Optional[Any]

Platform-specific context object (Lambda context, etc.).

mode
Optional[str]

Override the auto-detected execution mode.

  • "cgi" — CGI gateway interface
  • "lambda" — AWS Lambda
  • "google_cloud_function" — Google Cloud Functions
  • "azure_function" — Azure Functions

Returns

Platform-specific response:

  • Lambda: dict with statusCode, headers, and body
  • Google Cloud Functions: Flask Response object
  • Azure Functions: HttpResponse object
  • CGI: Response string printed to stdout

Examples

AWS Lambda

1from signalwire import AgentBase
2from signalwire.core.function_result import FunctionResult
3
4agent = AgentBase(name="lambda-agent")
5agent.set_prompt_text("You are a helpful assistant.")
6
7@agent.tool(description="Say hello")
8def say_hello(args, raw_data=None):
9 return FunctionResult("Hello!")
10
11def handler(event, context):
12 return agent.handle_serverless_request(event, context, mode="lambda")

Google Cloud Function

1from signalwire import AgentBase
2
3agent = AgentBase(name="gcf-agent")
4agent.set_prompt_text("You are a helpful assistant.")
5
6def main(request):
7 return agent.handle_serverless_request(request, mode="google_cloud_function")

Azure Function

1import azure.functions as func
2from signalwire import AgentBase
3
4agent = AgentBase(name="azure-agent")
5agent.set_prompt_text("You are a helpful assistant.")
6
7def main(req: func.HttpRequest) -> func.HttpResponse:
8 return agent.handle_serverless_request(req, mode="azure_function")