Skip to content

Commit cbb3a44

Browse files
committed
fix: fastapi a2a request body documentation
1 parent 1c8e12e commit cbb3a44

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import logging
22

3+
from collections.abc import AsyncIterator
4+
from contextlib import asynccontextmanager
35
from typing import Any
46

57
from fastapi import FastAPI, Request, Response
@@ -9,7 +11,7 @@
911
JSONRPCApplication,
1012
)
1113
from a2a.server.request_handlers.jsonrpc_handler import RequestHandler
12-
from a2a.types import AgentCard
14+
from a2a.types import A2ARequest, AgentCard
1315
from a2a.utils.constants import (
1416
AGENT_CARD_WELL_KNOWN_PATH,
1517
DEFAULT_RPC_URL,
@@ -70,7 +72,22 @@ def add_routes_to_app(
7072
extended_agent_card_url: The URL for the authenticated extended agent card endpoint.
7173
"""
7274

73-
@app.post(rpc_url)
75+
@app.post(
76+
rpc_url,
77+
openapi_extra={
78+
'requestBody': {
79+
'content': {
80+
'application/json': {
81+
'schema': {
82+
'$ref': '#/components/schemas/A2ARequest'
83+
}
84+
}
85+
},
86+
'required': True,
87+
'description': 'A2ARequest',
88+
}
89+
},
90+
)
7491
async def handle_a2a_request(request: Request) -> Response:
7592
return await self._handle_requests(request)
7693

@@ -104,7 +121,25 @@ def build(
104121
Returns:
105122
A configured FastAPI application instance.
106123
"""
107-
app = FastAPI(**kwargs)
124+
125+
@asynccontextmanager
126+
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
127+
a2a_request_schema = A2ARequest.model_json_schema(
128+
ref_template='#/components/schemas/{model}'
129+
)
130+
defs = a2a_request_schema.pop('$defs', {})
131+
openapi_schema = app.openapi()
132+
component_schemas = openapi_schema.setdefault(
133+
'components', {}
134+
).setdefault('schemas', {})
135+
component_schemas.update(defs)
136+
component_schemas['A2ARequest'] = a2a_request_schema
137+
138+
yield
139+
# Code to run on shutdown would go here
140+
141+
# Create app with lifespan handler
142+
app = FastAPI(lifespan=lifespan, **kwargs)
108143

109144
self.add_routes_to_app(
110145
app, agent_card_url, rpc_url, extended_agent_card_url

0 commit comments

Comments
 (0)