Skip to content

Commit ed6354e

Browse files
committed
Add utility script to export openapi.json
1 parent f08b375 commit ed6354e

File tree

4 files changed

+586
-17
lines changed

4 files changed

+586
-17
lines changed

pyproject.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ dev = [
88
"pytest>=8.4.0",
99
"pytest-asyncio>=1.0.0",
1010
"pytest-cov>=6.1.1",
11-
"httpx>=0.25.0"
11+
"httpx>=0.25.0",
12+
"hatch>=1.14.1",
13+
"pyyaml>=6.0.2",
1214
]
1315

1416
[project]
@@ -36,3 +38,9 @@ omit = ["tests/*"]
3638

3739
[tool.hatch.build.targets.wheel]
3840
packages = ["src/workflows"]
41+
42+
[tool.hatch.envs.server]
43+
features = ["server"]
44+
45+
[tool.hatch.envs.server.scripts]
46+
openapi = "python -m workflows.server.server --output openapi.json"

src/workflows/server/server.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from starlette.requests import Request
1616
from starlette.responses import JSONResponse, StreamingResponse
1717
from starlette.routing import Route
18+
from starlette.schemas import SchemaGenerator
1819

1920
from workflows import Context, Workflow
2021
from workflows.context.serializers import JsonSerializer
@@ -435,3 +436,33 @@ async def _extract_run_params(self, request: Request, workflow: Workflow) -> tup
435436
raise HTTPException(
436437
detail=f"Error processing request body: {e}", status_code=500
437438
)
439+
440+
def openapi_schema(self) -> dict:
441+
app = self.app
442+
gen = SchemaGenerator(
443+
{
444+
"openapi": "3.0.0",
445+
"info": {
446+
"title": "Workflows API",
447+
"version": "1.0.0",
448+
},
449+
}
450+
)
451+
452+
return gen.get_schema(app.routes)
453+
454+
455+
if __name__ == "__main__":
456+
import argparse
457+
458+
parser = argparse.ArgumentParser(description="Generate OpenAPI schema")
459+
parser.add_argument(
460+
"--output", type=str, default="openapi.json", help="Output file path"
461+
)
462+
args = parser.parse_args()
463+
464+
server = WorkflowServer()
465+
dict_schema = server.openapi_schema()
466+
with open(args.output, "w") as f:
467+
json.dump(dict_schema, indent=2, fp=f)
468+
# dump file

tests/server/test_openapi_schema.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# SPDX-License-Identifier: MIT
22
# Copyright (c) 2025 LlamaIndex Inc.
33

4-
from starlette.schemas import SchemaGenerator
54
from workflows import Workflow
65

76
from workflows.server import WorkflowServer
@@ -10,19 +9,7 @@
109
def test_openapi_schema_includes_all_routes(simple_test_workflow: Workflow) -> None:
1110
server = WorkflowServer()
1211
server.add_workflow("test", simple_test_workflow)
13-
14-
app = server.app
15-
gen = SchemaGenerator(
16-
{
17-
"openapi": "3.0.0",
18-
"info": {
19-
"title": "Workflows API",
20-
"version": "1.0.0",
21-
},
22-
}
23-
)
24-
25-
schema = gen.get_schema(app.routes)
12+
schema = server.openapi_schema()
2613

2714
assert "paths" in schema
2815
paths = schema["paths"]

0 commit comments

Comments
 (0)