fix(tanstack-ai): handle image ContentPart in workers-ai adapter#435
Open
mdhruvil wants to merge 2 commits intocloudflare:mainfrom
Open
fix(tanstack-ai): handle image ContentPart in workers-ai adapter#435mdhruvil wants to merge 2 commits intocloudflare:mainfrom
mdhruvil wants to merge 2 commits intocloudflare:mainfrom
Conversation
buildUserContent only handled type:"image_url" - TanStack AI sends type:"image" with source field, so image parts were silently dropped. - replace MessageLike with ModelMessage, ad-hoc content type with ContentPart - extract convertContentPart(): image source.type "url"/"data" → OpenAI image_url - guard against already-prefixed data URIs (matches reference OpenAI adapter) - warn + skip unsupported modalities (audio/video/document) - remove dead type:"image_url" codepath and type assertion - update tests to ContentPart format, add edge cases
🦋 Changeset detectedLatest commit: 561b1fc The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: Workers AI adapter drops
type: "image"content partsCloses #431
Problem
TanStack AI's multimodal content uses
ContentParttypes where images are represented as:The workers-ai adapter's
buildUserContentfunction only handled the legacytype: "image_url"format (workers-ai.ts#L98-L122), which TanStack AI never sends. This caused all image parts to be silently dropped from user messages.The detection logic on line 98 correctly checked for both
type: "image_url"andtype: "image", but the conversion logic on lines 112-122 only handledtype: "image_url"— so images were detected as present but never converted.Solution
Rewrote the message-building helpers to use TanStack AI's canonical types directly, following the patterns from the reference OpenAI adapter (as recommended by TanStack's community adapter guide).
Type changes
MessageLikeinterface — replaced withModelMessagefrom@tanstack/aiArray<{ type: string; content?: string; image_url?: unknown }>content type — replaced withModelMessage["content"]which isstring | null | Array<ContentPart>ContentPartandModelMessagefrom@tanstack/ai(already a peer dependency)convertContentPart(new)Extracted a dedicated function that converts a single
ContentPart→OpenAI.Chat.ChatCompletionContentPart:type: "text"{ type: "text", text: part.content }type: "image",source.type: "url"{ type: "image_url", image_url: { url: source.value } }type: "image",source.type: "data"{ type: "image_url", image_url: { url: "data:{mimeType};base64,{value}" } }audio/video/documentconsole.warn+ skip (Workers AI chat API doesn't support these)Includes a guard against
source.valuealready containing adata:prefix (matching the reference adapter's pattern) to prevent malformed double-prefixed URIs.Files changed
packages/tanstack-ai/src/adapters/workers-ai.tsContentPart/ModelMessage, handletype: "image"packages/tanstack-ai/test/message-builder.test.tsContentPartformatpackages/tanstack-ai/test/workers-ai-adapter.test.ts