Skip to content

Commit 95523a3

Browse files
committed
refactor!: Make the FastAPI dependency optional
With this update, using A2AFastAPIApplication requires either adding the FastAPI package directly to the project or indicating the "fastapi" group when installing a2a-sdk, e.g., with uv "uv add a2a-sdk[fastapi]" or with pip "pip install a2a-sdk[fastapi]"
1 parent a0bf13b commit 95523a3

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ authors = [{ name = "Google LLC", email = "[email protected]" }]
88
requires-python = ">=3.10"
99
keywords = ["A2A", "A2A SDK", "A2A Protocol", "Agent2Agent"]
1010
dependencies = [
11-
"fastapi>=0.115.2",
1211
"httpx>=0.28.1",
1312
"httpx-sse>=0.4.0",
1413
"google-api-core>=1.26.0",
@@ -80,6 +79,10 @@ dev = [
8079
"uv-dynamic-versioning>=0.8.2",
8180
"types-protobuf",
8281
"types-requests",
82+
"fastapi>=0.115.2",
83+
]
84+
fastapi = [
85+
"fastapi>=0.115.2",
8386
]
8487

8588
[[tool.uv.index]]

src/a2a/server/apps/jsonrpc/fastapi_app.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
from typing import Any
44

5-
from fastapi import FastAPI, Request, Response
6-
5+
from a2a.server.apps.jsonrpc.fastapi_import_helpers import (
6+
FastAPI,
7+
Request,
8+
Response,
9+
)
710
from a2a.server.apps.jsonrpc.jsonrpc_app import (
811
CallContextBuilder,
912
JSONRPCApplication,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""Helper functions for handling optional FastAPI package imports."""
2+
3+
try:
4+
from fastapi import FastAPI, Request, Response
5+
except ImportError:
6+
_FASTAPI_DEPENDENCY_ERROR_MSG = """
7+
The A2A Python SDK FastAPI app requires the FastAPI package, which
8+
is an optional dependency. To fix this issue, please add the fastapi
9+
package to your project, e.g. by executing
10+
11+
uv add fastapi
12+
13+
or install the A2A SDK with the optional FastAPI dependency:
14+
15+
uv add a2a-sdk[fastapi]
16+
"""
17+
18+
class _DummyFastAPIClasses:
19+
"""Parent class for dummy fastapi.* class declarations."""
20+
21+
def __init__(self) -> None:
22+
"""Raises ImportError when initiating a dummy fastapi.* instance."""
23+
raise ImportError(_FASTAPI_DEPENDENCY_ERROR_MSG)
24+
25+
class FastAPI(_DummyFastAPIClasses):
26+
"""A dummy fastapi.FastAPI declaration."""
27+
28+
class Request(_DummyFastAPIClasses):
29+
"""A dummy fastapi.Request declaration."""
30+
31+
class Response(_DummyFastAPIClasses):
32+
"""A dummy fastapi.Response declaration."""

src/a2a/server/apps/jsonrpc/jsonrpc_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from collections.abc import AsyncGenerator
88
from typing import Any
99

10-
from fastapi import FastAPI
1110
from pydantic import ValidationError
1211
from sse_starlette.sse import EventSourceResponse
1312
from starlette.applications import Starlette
@@ -17,6 +16,7 @@
1716

1817
from a2a.auth.user import UnauthenticatedUser
1918
from a2a.auth.user import User as A2AUser
19+
from a2a.server.apps.jsonrpc.fastapi_import_helpers import FastAPI
2020
from a2a.server.context import ServerCallContext
2121
from a2a.server.request_handlers.jsonrpc_handler import JSONRPCHandler
2222
from a2a.server.request_handlers.request_handler import RequestHandler

uv.lock

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)