|
|
2 недель назад | |
|---|---|---|
| .. | ||
| README.md | 2 недель назад | |
| client.py | 2 недель назад | |
| print_openapi_schema.py | 2 недель назад | |
| server.py | 2 недель назад | |
Full Guide: The canonical FastAPI documentation lives in
docs/additional-features/fastapi-integration.mdx. This README only summarizes the runnable sample.
This example demonstrates how to properly integrate Agency Swarm with FastAPI, including:
agent and callerAgent fields in eventsserver.py - FastAPI server that exposes an agency with two communicating agentsclient.py - Python client showing how to interact with the API endpointsInstall dependencies:
pip install agency-swarm[fastapi]
Set your OpenAI API key:
export OPENAI_API_KEY="your-key-here"
Optional: Set authentication token:
export APP_TOKEN="your-secret-token"
python server.py
The server will start on http://localhost:8080 with these endpoints:
POST /my-agency/get_response - Regular response endpointPOST /my-agency/get_response_stream - SSE streaming endpointGET /my-agency/get_metadata - Agency structure metadataSee the “Serving Standalone Tools” section in docs/additional-features/fastapi-integration.mdx for the full walkthrough. In short, calling run_fastapi(tools=[MyTool]) automatically exposes:
POST /tool/<ToolName> – executes the tool with validationGET /openapi.json – OpenAPI 3.1.0 schema for agencies + toolsGET /docs / GET /redoc – Swagger UI and ReDoc backed by the same schemaAll schemas include nested Pydantic models, so you can connect directly to platforms such as Agencii.ai.
python client.py
This will test all endpoints and show how to:
Run the helper script to confirm /openapi.json matches ToolFactory.get_openapi_schema():
python print_openapi_schema.py
The script prints the FastAPI /openapi.json response followed by the ToolFactory schema so you can diff them directly (no assertions or extra output).
The agent and callerAgent fields should appear in:
streaming_utils.py)new_messages array in both regular and streaming responsescall_id for correlationTo maintain conversation context:
chat_history: []new_messages to your chat historychat_history in the next requestIf APP_TOKEN is set, include it in requests:
headers = {"Authorization": f"Bearer {token}"}
response = requests.post(url, json=payload, headers=headers)
text/event-stream content typeproxy_buffering off)data: prefixchat_history is a flat list of message dictionariesrole, content/text, agent, callerAgentload_threads_callback to the agency factory