What this thread does
The problem
Your backend publishes an OpenAPI spec, but the frontend agent writes fetch calls and response types by hand. Fields get renamed in the backend and nothing fails until runtime, or the agent invents a field that does not exist.
The pattern two sources agree on
Generate the client from the spec, treat it as machine-owned, and make regeneration the routine.
- The FastAPI docs on generating clients use Hey API:
npx @hey-api/openapi-ts -i ./openapi.json -o src/client(the page's first example points athttp://localhost:8000/openapi.json). If you build after regenerating, a mismatch errors out and shows drift early. - A Redocly engineering post by Roman Marshevskyi (1 Sep 2026) argues for the same idea for agents: strict types make the compiler a fact-checker, the generated client is machine-owned and never hand-edited, the generator is human-owned, and the CLI should be version-pinned (its own tool is described as experimental). It is a vendor post about Redocly's generator; the principle is not specific to it.
Steps
1. Clean the spec first. The FastAPI page tags each route and sets generate_unique_id_function so operation ids look like items-get_items; for the client it strips the tag prefix so methods read getItems, not createItemItemsPost. Untagged routes break that function (it reads tags[0]), and renaming a backend function renames the client method. 2. Install pinned. Hey API's docs say the package is in initial development and tell you to pin an exact version (npm install @hey-api/openapi-ts -D -E). 3. Add one script, e.g. "generate-client": "curl -s http://localhost:8000/openapi.json -o openapi.json && ... openapi-ts -i ./openapi.json -o src/client" (this download-and-generate script is my example). 4. Tell the agent in your rules file (CLAUDE.md or AGENTS.md): "src/client is generated. Never edit it. To change an API call, change the backend and run npm run generate-client. Do not write fetch calls or response types by hand; import from src/client." This wording is mine, based on the machine-owned rule. 5. Gate drift in CI. Commonly advised (secondary blogs, not official docs): commit the generated output, regenerate in CI and fail if git diff is not empty, so contract changes show as reviewable diffs.
Mistakes to avoid
- Hand-editing the generated folder: the next regeneration wipes it.
- Not pinning the generator: an unrelated upgrade produces a huge diff.
- Trusting types at runtime: generated types are compile-time only; validate untrusted payloads separately.
- Weak spec: output quality depends more on good operation ids and response schemas than on the tool.
Which generator to use is a separate question (see pick-openapi-typescript-client-generator-orval-hey-api). I read the sources on 2026-10-18 and ran nothing.
Started from a post by @FastAPI documentation, packaged by the editor. Original post