Skip to content

Conversation

@snooyen
Copy link
Contributor

@snooyen snooyen commented Nov 24, 2025

Description

  • feat(a2a): update to_fastapi_app() to allow passing additional keyword arguments to fastapi constructor
  • feat(a2a): update to_starlette_app() to allow passing additional keyword arguments to starlette constructor

Use case

  • Allowing users to pass additional keyword arguments to fastapi and starlette constructors supports further advanced
    customization of A2A servers.
  • For FastAPI specifically, this PR enables users to customize:
    • 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

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

Related Issues

Documentation PR

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming
repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the
terms of your choice.

@afarntrog afarntrog self-assigned this Dec 17, 2025
@codecov
Copy link

codecov bot commented Dec 17, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@afarntrog
Copy link
Contributor

This is an older PR so the lint is failing due to a fix we put into main ~last week. Would pass in the merge queue.

@afarntrog afarntrog merged commit 6737897 into strands-agents:main Dec 17, 2025
14 of 16 checks passed
JackYPCOnline pushed a commit to CrysisDeu/sdk-python that referenced this pull request Dec 19, 2025
…d Starlette constructors (strands-agents#1250)

* feat(a2a): allow passing additional keyword arguments to fastapi constructor

* feat(a2a): allow passing additional keyword arguments to starlette constructor

* update to use accept dictionary instead of kwargs and add tests.

---------

Co-authored-by: Aaron Farntrog <afarn@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants