Skip to content

Proposed Auto-generated types library for fal-ai/client#204

Open
tombeckenham wants to merge 17 commits intofal-ai:mainfrom
tombeckenham:184-fal-js-client-out-of-sync-with-fal-api
Open

Proposed Auto-generated types library for fal-ai/client#204
tombeckenham wants to merge 17 commits intofal-ai:mainfrom
tombeckenham:184-fal-js-client-out-of-sync-with-fal-api

Conversation

@tombeckenham
Copy link
Copy Markdown

@tombeckenham tombeckenham commented Mar 27, 2026

This PR provides a new way of generating types for @fal-ai/client by providing an independent @fal-ai/types library that is auto-generated from live Fal OpenAPI specs. Add a FAL_KEY to .env.local then:

npm run update-types

I researched type safety extensively on the Fal client while building the TanStack AI adapter for Fal. When the TanStack team built the adapters for other providers, they found it was nearly impossible to have a single endpoint type that covered all models for all content types. See their blog post on why they split the adapters.

Likewise in Fal client, having a single EndpointTypeMap for every single model, has already hit the limit of what's possible.

I ran an experiment to see if it was possible to do the same by category of output. I forked the fal-js client library, and created a types library that uses HeyApi to generate types & schemas by category. HeyApi is brilliant for this, and is well maintained.

I found it was possible to generate the EndpointTypeMaps per category by combining OpenAPI specs downloaded from Fal. This gives us an InputType and OutputType by category. I then found it was possible to provide a single InputType and OutputType using long conditionals like this:

export type InputType<T extends EndpointType> = T extends Gen3dModel
  ? Gen3dModelInput<T>
  : T extends AudioModel
    ? AudioModelInput<T>
    : T extends ImageModel
      ? ImageModelInput<T>
      : T extends JsonModel
        ? JsonModelInput<T>
        : T extends LlmModel
          ? LlmModelInput<T>
          : T extends SpeechModel
            ? SpeechModelInput<T>
            : T extends TextModel
              ? TextModelInput<T>
              : T extends TrainingModel
                ? TrainingModelInput<T>
                : T extends UnknownModel
                  ? UnknownModelInput<T>
                  : T extends VideoModel
                    ? VideoModelInput<T>
                    : T extends VisionModel
                      ? VisionModelInput<T>
                      : never;

Key files:

  • scripts/fetch-openapi-models.ts - uses a FAL_KEY to download the OpenAPI spec for every model then combine by category into a hidden json folder
  • openapi-ts.config.ts - HeyApi config that transforms (corrects) and merges OpenAPI specs - then generates types and zod schemas from those specs. Note, it could generate json schemas as well, but you can generate them from the zod
  • scripts/generate-endpoint-maps.ts - Generates the endpoint type maps and discriminatedUnion zod schemas for each category then creates a combined index for the conditional EndpointType

Closes #184
Closes #195

tombeckenham and others added 17 commits February 3, 2026 12:08
…package

Create a new @fal-ai/types package to decouple endpoint type definitions
from the client, allowing types to update more frequently than the client.

- Create libs/types/ with package.json, project.json, and tsconfig files
- Move all generated type directories (3d, audio, image, etc.) to types package
- Move endpoints.ts, client.ts, schemas.ts aggregation files to types package
- Update client to import EndpointType/InputType/OutputType from @fal-ai/types
- Add @fal-ai/types as optional peer dependency of @fal-ai/client
- Re-export type aliases from client for backwards compatibility
- Update openapi-ts.config.ts and generate-endpoint-maps.ts output paths
- Update tsconfig.base.json with @fal-ai/types path mappings
- Fix pre-existing issue with missing json category references

https://claude.ai/code/session_01EudHCRh6N1AjX1RRDG6RJj
…ut-of-sync-with-fal-api and update generation
Generate index.ts instead of client.ts as the types package entry point,
make @fal-ai/types a required dependency (>=0.1.0 <1.0.0), add nx
implicit dependency for build ordering, and coerce mismatched default
values in OpenAPI specs to prevent Zod codegen type errors.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@tombeckenham tombeckenham changed the title 184 fal js client out of sync with fal api Proposed Auto-generated types library for fal-ai/client Mar 27, 2026
@mrlubos
Copy link
Copy Markdown

mrlubos commented Mar 29, 2026

@tombeckenham wow what a massive pull request. Let me know if you need anything from @hey-api!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Split out types into its own package fal-js client out of sync with fal api

3 participants