print_openapi_schema.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """
  2. Utility script that prints FastAPI's /openapi.json schema alongside
  3. ToolFactory.get_openapi_schema() for the ExampleTool shown in server.py.
  4. Run:
  5. python print_openapi_schema.py
  6. """
  7. import json
  8. import os
  9. import sys
  10. from pydantic import BaseModel, Field
  11. # Path setup for standalone examples
  12. sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "src")))
  13. sys.path.insert(0, os.path.dirname(__file__))
  14. from server import create_agency # noqa: E402
  15. from agency_swarm import BaseTool, run_fastapi # noqa: E402
  16. from agency_swarm.tools import ToolFactory # noqa: E402
  17. class GreetingRequest(BaseModel):
  18. name: str = Field(..., description="Name of the recipient.")
  19. address: str = Field(..., description="Mailing address that can be echoed back.")
  20. class GreetingOptions(BaseModel):
  21. greeting_type: str = Field("Hello", description="Prefix used in the greeting message.")
  22. include_address: bool = Field(
  23. True,
  24. description="When true, the greeting includes the recipient's address.",
  25. )
  26. class ExampleTool(BaseTool):
  27. """Generate greetings with optional address metadata."""
  28. recipient: GreetingRequest
  29. options: GreetingOptions = GreetingOptions()
  30. def run(self) -> str:
  31. message = f"{self.options.greeting_type}, {self.recipient.name}!"
  32. if self.options.include_address:
  33. message += f" (Address: {self.recipient.address})"
  34. return message
  35. def main() -> None:
  36. app = run_fastapi(agencies={"my-agency": create_agency}, tools=[ExampleTool], port=8080, return_app=True)
  37. fastapi_schema = app.openapi()
  38. factory_schema = json.loads(ToolFactory.get_openapi_schema([ExampleTool], "http://localhost:8080"))
  39. print("FASTAPI_OPENAPI_SCHEMA:")
  40. print(json.dumps(fastapi_schema, indent=2))
  41. print("FACTORY_OPENAPI_SCHEMA:")
  42. print(json.dumps(factory_schema, indent=2))
  43. if __name__ == "__main__":
  44. main()