Skip to content

[FEATURE] Add support for passing keyword arguments to A2A FastAPI and Starlette constructors #1249

@snooyen

Description

@snooyen

Problem Statement

The Strands SDK should allow users to pass additional keyword arguments to fastapi and starlette constructors supports further advanced customization of A2A servers.

Proposed Solution

The A2A Python SDK A2AFastAPI and A2AStarletteApplication classes both provide build methods which expose **kwarg parameters. The respective to_fastapi_app and to_starlette_app methonds on the Strands SDK A2AServer class simply need to be updated to enable passing this through to the A2A Python SDK build methods.

Example code:

from contextlib import asynccontextmanager
from fastapi import FastAPI
from strands import Agent
from strands.multiagent.a2a import A2AServer

def initialize_app() -> FastAPI:
    """Initialize and configure FastAPI application."""

    @asynccontextmanager
    async def lifespan(app: FastAPI):
        """Manage application lifespan with proper error handling."""
        # Startup tasks
        yield  # Application runs here
        # Shutdown tasks

    # Create your agent and A2A server
    agent = Agent(name="My Agent", description="A customizable agent", callback_handler=None)
    a2a_server = A2AServer(agent=agent)

    serve_docs_enabled = False

    app = a2a_server.to_fastapi_app(
        title=config.app_name,
        docs_url="/docs" if serve_docs_enabled else None,
        redoc_url="/redoc" if serve_docs_enabled else None,
        openapi_url="/openapi.json" if serve_docs_enabled else None,
        debug=config.debug,
        lifespan=lifespan,
    )

    return app

Use Case

Enables users to further customize A2AServers as needed:

  • Startup / Shutdown events by passing a lifespan function to the FastAPI constructor. This is preferable to using the now deprecated startup/shutdown event handlers.
  • enabling/disabling documentation endpoints
  • enabling/disabling debug tracebacks

Alternatives Solutions

No response

Additional Context

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions