dify-ai-provider
A Vercel AI SDK provider that turns any Dify.AI application into a drop-in language model.
Repository Health
Technical Analysis
dify-ai-provider is a Vercel AI SDK community provider that lets you plug a Dify.AI application straight into any Vercel AI SDK-based app as a LanguageModelV2. Instead of hand-rolling a fetch wrapper around Dify’s chat-messages API, you get generateText and streamText support out of the box, with conversation continuity handled through simple user-id and chat-id request headers.
Because Dify’s API has no native function/tool calling, the provider works around it by injecting tool definitions into the outgoing prompt and parsing structured JSON tool calls back out of the model’s streamed answer text, including think-tag stripping for reasoning-style models and automatic file/image upload for multimodal messages. It also supports pointing at a self-hosted Dify instance via a custom baseURL, making it useful both for teams on Dify Cloud and those running Dify themselves.
What You Get
- Full generateText and streamText support against Dify’s chat-messages endpoint, in both blocking and streaming response modes.
- Automatic conversation continuity via chat-id/user-id headers, so multi-turn chats map cleanly onto Dify conversations.
- A prompt-injection tool-calling shim (formatToolsPrompt/parseToolCalls) that lets AI SDK tools work even though Dify’s API has no native function-calling support.
- Support for self-hosted Dify deployments via a configurable baseURL, plus file/image attachment upload and <think> reasoning-tag parsing for reasoning models.
Common Use Cases
- Wiring a Dify-built chatbot or agent into a Next.js AI Chatbot-style frontend without switching away from the Vercel AI SDK’s streaming primitives.
- Giving a self-hosted Dify instance the same generateText/streamText call surface as OpenAI or Anthropic models already used elsewhere in an app.
- Building tool-using agents on top of Dify workflows that don’t natively support the AI SDK’s function-calling contract.
- Migrating an existing Vercel AI SDK app to route specific requests to a Dify application without rewriting the calling code.
Under The Hood
Architecture
The package is split into a small set of focused modules: index.ts (barrel export), dify-provider.ts (the createDifyProvider/difyProvider factory and settings types), dify-chat-language-model.ts (the core LanguageModelV2 implementation), dify-chat-schema.ts (Zod schemas for Dify’s REST and SSE event shapes via discriminatedUnion), dify-chat-settings.ts (shared types and the default logger), and stream-parser.ts (think-tag parsing, tool-call scraping, tool-prompt formatting, and file-upload helpers). This keeps protocol-specific parsing out of the model class itself. The provider factory constructs DifyChatLanguageModel instances with a resolved baseURL and header function; getRequestBody builds a single Dify query string by walking conversation history (buildQuery/extractSystemPrompt/findConversationId), and doStream’s TransformStream converts Dify’s SSE event stream into standard LanguageModelV2StreamPart events using small state flags (isActiveText/isActiveReasoning). A future change to the AI SDK’s LanguageModelV2 contract would require matching updates across this fairly large (~680-line) model class.
Tech Stack
TypeScript targeting ES2018 with strict mode enabled; runtime dependencies are @ai-sdk/provider and @ai-sdk/provider-utils (for the LanguageModelV2 types and HTTP/SSE helpers like postJsonToApi and createEventSourceResponseHandler) plus zod for request/response validation. The package is built with tsup into dual CJS/ESM bundles with generated .d.ts files, tested with vitest, and uses pnpm for dependency management. It is a headless library consumed by apps already using the Vercel AI SDK, communicating with Dify’s chat-messages and file-upload REST/SSE endpoints over native fetch (with an injectable custom fetch for testing).
Code Quality
The repo includes real test coverage — a large model-level test suite and a dedicated suite for the think-tag and tool-call parser — plus a manual debug script for interactively inspecting Dify’s SSE stream. Strict TypeScript and Zod runtime validation catch shape mismatches that plain types would miss, and errors are surfaced explicitly through APICallError and a JSON error-response handler rather than being swallowed, though a few helper functions (like file upload) log and return undefined on failure instead of propagating the error. No CI workflow actually runs the test suite — the only GitHub Actions configured are Claude Code review/assistant workflows — and no linter/formatter config was found.
API Design
The public surface is intentionally small: a single difyProvider/createDifyProvider factory returns a model that behaves exactly like any other Vercel AI SDK provider, so switching an app to a Dify-backed model is a one-line change. Settings are consolidated into two documented option objects, and self-hosted Dify is handled through the same code path as Dify Cloud (just a different baseURL) rather than a separate integration. The one DX gap is that the prompt-injection tool-calling workaround is undocumented in the README, so enabling tools on a call gets non-native behavior with no explicit signal to the caller.
Used by 2 apps in this directory
Cline
AI Code Assistants
An open-source AI coding agent that lives in your editor and terminal — reads and edits your codebase, runs commands, browses the web, and requires human approval for every action by default.
Typebot
Developer Tools · AI Development · No Code Platforms
Build sophisticated chatbots visually, embed them anywhere without iframes, and own your data — fully self-hostable with a modular block system and 30+ integrations.