openapi-schemas.mdx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. ---
  2. title: "OpenAPI Schemas"
  3. description: "Convert OpenAPI schemas into tools."
  4. icon: "brackets-curly"
  5. ---
  6. Agency allows you to easily convert OpenAPI schemas into tools so your agents can interact with any external APIs. For example, by adding the Google Calendar API schema, your agent will be able to create, update, delete, and retrieve events from Google Calendar.
  7. <Tip>
  8. It is still recommended to create custom tools and wrap each API call into a `BaseTool` class, even if you have the OpenAPI schema. OpenAPI schemas allow you to get started quickly, however, for production, you might want to add some custom data validation, error handling, data processing or even combine multiple API calls into a single tool.
  9. </Tip>
  10. ## How to Find OpenAPI Schemas
  11. The recommended way to create OpenAPI schemas is to use [Actions GPT](https://chatgpt.com/g/g-TYEliDU6A-actionsgpt). Simply ask it to create a schema for the API you want to use and which actions you want to perform.
  12. **If your API is public and well known**, it should be able to create a schema for you on the first try, without any extra documentation.
  13. ```
  14. Create a schema for the Google Calendar API and include the following actions: create, update, delete, and get events.
  15. ```
  16. **If your API is public but not well known**, we recommend searching for the API documentation manually and then sending a link to your API into the prompt:
  17. ```
  18. Create a schema for the following API: https://api.example.com/openapi.json and include the following actions: create, update, delete, and get events.
  19. ```
  20. **If you your API is private**, you can attach your API documentation in a file:
  21. ```
  22. Create a schema for the API documentation attached in the file. Include the following actions: create, update, delete, and get events.
  23. ```
  24. ## How to Use OpenAPI Schemas
  25. Below are the two ways to use OpenAPI schemas in your agents:
  26. #### Option 1: Using the `schemas_folder`
  27. The first way to integrate OpenAPI schemas is by placing all your OpenAPI schema files in a folder, and then initializing your agent with the `schemas_folder` parameter. Agency Swarm will then automatically scan this folder and convert any OpenAPI schemas it finds into `BaseTool` instances.
  28. ```python
  29. from agency_swarm import Agent
  30. agent = Agent(
  31. name='MyAgent',
  32. schemas_folder='schemas',
  33. api_params={'api_schema.json': {'param1': 'value1'}},
  34. api_headers={'api_schema.json': {'Authorization': 'Bearer token'}}
  35. )
  36. ```
  37. In this example:
  38. - `schemas_folder`: Directory where your OpenAPI schema files are stored.
  39. - `api_params`: Extra parameters for specific schemas.
  40. - `api_headers`: Custom headers for API calls, like authentication tokens.
  41. #### Option 2: Using the ToolFactory Class
  42. Alternatively, you can use the `ToolFactory` class to convert OpenAPI schemas from local files or URLs.
  43. ```python
  44. from agency_swarm.tools import ToolFactory
  45. tools = ToolFactory.from_openapi_schema(
  46. "<your OpenAPI schema here>",
  47. headers={'Authorization': 'Bearer token'},
  48. params={'param1': 'value1'},
  49. strict=False
  50. )
  51. ```
  52. <Accordion title="Converting from a Local Schema File">
  53. ```python
  54. from agency_swarm.tools import ToolFactory
  55. with open("schemas/api_schema.json") as f:
  56. tools = ToolFactory.from_openapi_schema(f.read())
  57. ```
  58. </Accordion>
  59. <Accordion title="Converting from a Remote Schema URL">
  60. ```python
  61. from agency_swarm.tools import ToolFactory
  62. import requests
  63. response = requests.get("https://api.example.com/openapi.json")
  64. tools = ToolFactory.from_openapi_schema(response.json())
  65. ```
  66. </Accordion>
  67. Argument descriptions:
  68. - `schema`: The OpenAPI schema to convert.
  69. - `headers`: HTTP headers applied to every request generated from the schema. For per-file mappings when using `Agent(schemas_folder=...)`, pass them via the agent's `api_headers` argument.
  70. - `params`: Query parameters appended to every request generated from the schema.
  71. - `strict`: Whether to use strict OpenAI mode.
  72. To add your tools to your agent with the 2nd option, simply pass the `tools` list to your agent:
  73. ```python
  74. agent = Agent(
  75. name='MyAgent',
  76. tools=tools
  77. )
  78. ```
  79. With this approach, you have more control over the tools you are adding to your agent, and you can still modify the `ToolConfig` of each tool. See the [ToolConfig documentation](/core-framework/tools/custom-tools/configuration) for more information.
  80. <Info>
  81. With any of these methods, Agency still converts your schemas into PyDantic models, so your agents will perform type checking on all API parameters **before** making API calls, reducing errors and improving reliability.
  82. </Info>