{"id":31608721,"url":"https://github.com/nakasyou/ai-fallback","last_synced_at":"2026-05-05T14:04:45.514Z","repository":{"id":295339186,"uuid":"963172713","full_name":"nakasyou/ai-fallback","owner":"nakasyou","description":"Improve AI reliability with automatic fallback across configured models/providers with AI SDK.","archived":false,"fork":false,"pushed_at":"2025-05-25T01:10:47.000Z","size":12,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-25T01:33:29.213Z","etag":null,"topics":["ai","ai-sdk","aisdk","bun","deno","llm","nodejs","typescript","vercel-ai-sdk"],"latest_commit_sha":null,"homepage":"https://jsr.io/@ns/ai-fallback","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nakasyou.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-04-09T09:14:22.000Z","updated_at":"2025-05-25T01:10:51.000Z","dependencies_parsed_at":"2025-05-25T01:34:50.738Z","dependency_job_id":"2392437e-64ac-4d4b-b586-5414a741150f","html_url":"https://github.com/nakasyou/ai-fallback","commit_stats":null,"previous_names":["nakasyou/ai-fallback"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nakasyou/ai-fallback","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nakasyou%2Fai-fallback","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nakasyou%2Fai-fallback/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nakasyou%2Fai-fallback/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nakasyou%2Fai-fallback/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nakasyou","download_url":"https://codeload.github.com/nakasyou/ai-fallback/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nakasyou%2Fai-fallback/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278578131,"owners_count":26009739,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-06T02:00:05.630Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","ai-sdk","aisdk","bun","deno","llm","nodejs","typescript","vercel-ai-sdk"],"created_at":"2025-10-06T08:23:54.829Z","updated_at":"2025-10-06T08:23:56.994Z","avatar_url":"https://github.com/nakasyou.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!-- Thanks to Gemini 2.0 Pro Preview 03-25 --\u003e\n\n# @ns/ai-fallback\n\n[![JSR Version](https://jsr.io/badges/@ns/ai-fallback)](https://jsr.io/@ns/ai-fallback)\n\n\u003c!-- Note: Update this if the JSR name changes --\u003e\n\nA utility library to create resilient AI model chains using the AI SDK provider\ninterface (`@ai-sdk/provider`). This library allows you to define a sequence of\nmodels, providing automatic **fallback** capabilities.\n\nWhen you make a request to a model created with this library, it attempts the\nrequest with the first model in the sequence. If that attempt fails (due to\ntemporary API errors, rate limits, etc.), it automatically retries the _same_\nrequest with the next model in the chain, continuing until a request succeeds or\nall models have been attempted.\n\nThis significantly improves the robustness of your AI integrations by gracefully\nhandling transient issues with specific providers or models.\n\nSupports creating fallback chains for:\n\n- Language Models (`LanguageModelV1`) using `combineLanguageModels`\n- Embedding Models (`EmbeddingModelV1`) using `combineEmbeddings`\n- Transcription Models (`TranscriptionModelV1`) using `combineTranscriptions`\n- Image Models (`ImageModelV1`) using `combineImages`\n\n## Installation\n\nThis package is hosted on [JSR](https://jsr.io/@ns/ai-fallback), the JavaScript\nRegistry. \u003c!-- Note: Update this if the JSR name changes --\u003e\n\n**Deno:**\n\n```bash\ndeno add @ns/ai-fallback\n```\n\n**Bun:**\n\n```bash\nbunx jsr add --bun @ns/ai-fallback\n```\n\n**pnpm:**\n\n```bash\npnpm dlx jsr add @ns/ai-fallback\n```\n\n**Yarn:**\n\n```bash\nyarn dlx jsr add @ns/ai-fallback\n```\n\n**npm:**\n\n```bash\nnpx jsr add @ns/ai-fallback\n```\n\nYou will also need the base `ai` package and provider-specific packages (e.g.,\n`@ai-sdk/openai`, `@ai-sdk/google`).\n\n## Usage\n\nHere's a simple example creating a language model chain with OpenAI's GPT-4o as\nthe primary and Google's Gemini Flash as the fallback. If the request to GPT-4o\nfails, the library automatically retries with Gemini Flash.\n\n```typescript\nimport { generateText } from 'ai'\n// Import the function to create the fallback chain\nimport { combineLanguageModels } from '@ns/ai-fallback'\nimport { openai } from '@ai-sdk/openai'\nimport { google } from '@ai-sdk/google'\n\n// Define the model sequence for fallback.\n// Requests will first try gpt-4o. If it fails (e.g., API error, rate limit),\n// the *same* request will automatically be sent to the next model (gemini-2.0-flash).\nconst modelWithFallback = combineLanguageModels([\n  openai('gpt-4o'),\n  // Ensure 'gemini-2.0-flash' is a valid and accessible model ID for your setup\n  google('gemini-2.0-flash'),\n])\n\n// Use the fallback-enabled model just like any other AI SDK model.\n// Top-level await is used for simplicity in this example.\nconst { text } = await generateText({\n  model: modelWithFallback, // Use the model chain here\n  prompt: 'Why is the sky blue? Explain concisely.',\n})\n\nconsole.log(text)\n\n// In a real application, you would likely add error handling\n// for the case where *all* models in the fallback sequence fail.\n```\n\nSimilar functions (`combineEmbeddings`, `combineTranscriptions`,\n`combineImages`) are available for creating fallback chains for other types of\nAI models. They follow the same pattern: pass an array of compatible models in\nyour desired fallback order, and use the returned model object with the\ncorresponding AI SDK function (`embed`, `transcribe`, `generateImage`).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnakasyou%2Fai-fallback","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnakasyou%2Fai-fallback","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnakasyou%2Fai-fallback/lists"}