Skip to content
Source
Sign inSign up
TanStack/ai

feat: split up adapters into functionalities and add generate function #137

MergedAlemTuzlakmerged 41 commits intomain fromrefs/pull/137/headmerged by AlemTuzlak View on GitHub

AlemTuzlakcommented

🎯 Changes

Split adapters into:

  • text
  • embed
  • summarize

Added in generate function, how do we handle this from ehre? Do we want to deprecate the old openai() approach?

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • New Features

    • Split up adapters for better tree shaking into separate functionalities
    • Added embeddings, summarization, image generation, text-to-speech, and transcription support across providers
    • Added experimental video generation support (OpenAI)
  • Bug Fixes & Improvements

    • Enhanced type safety with improved model-specific options
    • Improved streaming and tool handling
  • Documentation

    • Updated guides for new adapters and features
    • Added tree-shaking guide and examples
✏️ Tip: You can customize this high-level summary in your review settings.
    1. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit d1abe4da3ab819aaa5a8b058e1d113a628a161b9

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded8sView ↗
      nx run-many --targets=build --exclude=examples/**✅ Succeeded1sView ↗

      ☁️ Nx Cloud last updated this comment at 2025-12-19 19:44:53 UTC

    2. pkg-pr-new[bot](GitHub profile, opens in a new tab)commented

      Open in StackBlitz

      @tanstack/ai

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai@137

      @tanstack/ai-anthropic

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-anthropic@137

      @tanstack/ai-client

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-client@137

      @tanstack/ai-devtools-core

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-devtools-core@137

      @tanstack/ai-gemini

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-gemini@137

      @tanstack/ai-ollama

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-ollama@137

      @tanstack/ai-openai

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openai@137

      @tanstack/ai-react

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react@137

      @tanstack/ai-react-ui

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react-ui@137

      @tanstack/ai-solid

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid@137

      @tanstack/ai-solid-ui

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid-ui@137

      @tanstack/ai-svelte

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-svelte@137

      @tanstack/ai-vue

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue@137

      @tanstack/ai-vue-ui

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue-ui@137

      @tanstack/react-ai-devtools

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/react-ai-devtools@137

      @tanstack/solid-ai-devtools

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/solid-ai-devtools@137

      commit: d1abe4d

    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Important

      Review skipped

      More than 25% of the files skipped due to max files limit. The review is being skipped to prevent a low-quality review.

      89 files out of 226 files are above the max files limit of 100. Please upgrade to Pro plan to get higher limits.

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Walkthrough

      This pull request refactors the TanStack AI framework to support tree-shakeable adapters. The primary change splits monolithic provider adapters (e.g., openai, anthropic) into separate, modular adapters for each capability (text, embedding, summarization, image generation, TTS, video, transcription). Renamed core types (ChatOptions → TextOptions), updated example imports and usage across the codebase, and added comprehensive activity-based modules to support the new modular architecture. The framework now supports selective imports for smaller bundles, improved type safety per adapter and model, and a cleaner, more extensible API surface.

      Changes

      Cohort / File(s)Summary
      Changelog & Versioning
      .changeset/two-bikes-kneel.md
      Bumps minor versions for all AI provider packages and core AI package with changelog note about tree-shakeable adapter split.
      Core Type Renames & Infrastructure
      packages/typescript/ai/src/types.ts, packages/typescript/ai/src/base-adapter.ts, packages/typescript/ai/src/event-client.ts
      Renamed ChatOptions → TextOptions, ChatCompletionChunk → TextCompletionChunk, and related stream option types; updated event client keys from chat:* to text:*.
      Core Adapter Abstractions (Activities)
      packages/typescript/ai/src/activities/chat/adapter.ts, packages/typescript/ai/src/activities/embedding/adapter.ts, packages/typescript/ai/src/activities/summarize/adapter.ts, packages/typescript/ai/src/activities/generateImage/adapter.ts, packages/typescript/ai/src/activities/generateVideo/adapter.ts, packages/typescript/ai/src/activities/generateSpeech/adapter.ts, packages/typescript/ai/src/activities/generateTranscription/adapter.ts
      Added base adapter classes and interfaces for each activity type (text, embedding, summarize, image, video, TTS, transcription) with config and inheritance support.
      Core Activity Implementations
      packages/typescript/ai/src/activities/chat/index.ts, packages/typescript/ai/src/activities/chat/messages.ts, packages/typescript/ai/src/activities/embedding/index.ts, packages/typescript/ai/src/activities/summarize/index.ts, packages/typescript/ai/src/activities/generateImage/index.ts, packages/typescript/ai/src/activities/generateVideo/index.ts, packages/typescript/ai/src/activities/generateSpeech/index.ts, packages/typescript/ai/src/activities/generateTranscription/index.ts
      Implemented full activity workflows with event emission, type-safe option handling, streaming support (where applicable), and adapter integration.
      Activity Option Factory
      packages/typescript/ai/src/activity-options.ts, packages/typescript/ai/src/activities/index.ts
      Added factory functions (createChatOptions, createEmbeddingOptions, etc.) for typed option construction and centralized activity map/exports.
      Stream & Message Utilities
      packages/typescript/ai/src/stream-to-response.ts, packages/typescript/ai/src/activities/chat/stream/*, packages/typescript/ai/src/activities/chat/tools/*
      Added streamToText() utility; reorganized stream processing, message updating, and tool-related code into activities structure.
      OpenAI Adapters (Text, Embed, Summarize, Image, Video, TTS, Transcription)
      packages/typescript/ai-openai/src/adapters/{text,embed,summarize,image,video,tts,transcription}.ts, packages/typescript/ai-openai/src/image/image-provider-options.ts, packages/typescript/ai-openai/src/video/video-provider-options.ts, packages/typescript/ai-openai/src/audio/{tts,transcription}-provider-options.ts, packages/typescript/ai-openai/src/utils/{client,schema-converter}.ts
      Implemented separate tree-shakeable adapters for each OpenAI capability with provider-specific options, model validation, streaming support, and factory helpers.
      OpenAI Index & Exports
      packages/typescript/ai-openai/src/index.ts, packages/typescript/ai-openai/src/model-meta.ts
      Replaced monolithic export with modular adapter exports for text, embed, summarize, image, video, TTS, transcription; added model lists (OPENAI_IMAGE_MODELS, OPENAI_VIDEO_MODELS, OPENAI_TTS_MODELS, OPENAI_TRANSCRIPTION_MODELS).
      Anthropic Adapters (Text, Summarize)
      packages/typescript/ai-anthropic/src/adapters/{text,summarize}.ts, packages/typescript/ai-anthropic/src/utils/client.ts
      Refactored monolithic adapter into separate text and summarize adapters with new naming (AnthropicTextAdapter, anthropicText); added shared client utilities.
      Anthropic Index & Exports
      packages/typescript/ai-anthropic/src/index.ts
      Replaced Anthropic class export with AnthropicTextAdapter and AnthropicSummarizeAdapter exports; preserved model metadata and tool conversion utilities.
      Gemini Adapters (Text, Embed, Summarize, Image, TTS)
      packages/typescript/ai-gemini/src/adapters/{text,embed,summarize,image,tts}.ts, packages/typescript/ai-gemini/src/image/image-provider-options.ts, packages/typescript/ai-gemini/src/utils/client.ts
      Implemented separate Gemini adapters for text, embedding, summarization, image generation, and TTS with provider-specific options and factory helpers.
      Gemini Index & Model Meta
      packages/typescript/ai-gemini/src/index.ts, packages/typescript/ai-gemini/src/model-meta.ts
      Replaced monolithic GeminiAdapter with modular exports; exposed image and TTS model lists; added comprehensive provider option type exports.
      Ollama Adapters (Text, Embed, Summarize)
      packages/typescript/ai-ollama/src/adapters/{text,embed,summarize}.ts, packages/typescript/ai-ollama/src/utils/client.ts
      Implemented tree-shakeable text, embed, and summarize adapters for Ollama with host-aware client creation and token estimation.
      Ollama Index & Adapter Removal
      packages/typescript/ai-ollama/src/index.ts, packages/typescript/ai-ollama/src/ollama-adapter.ts (deleted)
      Replaced monolithic ollama-adapter.ts with modular adapter exports; removed legacy Ollama class.
      Core Package Re-exports
      packages/typescript/ai/src/index.ts
      Reorganized exports to expose new activity functions (text, embedding, summarize, image, video, TTS, transcription), tool definitions, and deprecated legacy chat/embedding/summarize exports for compatibility.
      Client-Side Hooks (React, Solid, Vue, Svelte)
      packages/typescript/ai-react/src/use-chat.ts, packages/typescript/ai-solid/src/use-chat.ts, packages/typescript/ai-vue/src/use-chat.ts, packages/typescript/ai-svelte/src/create-chat.svelte.ts, packages/typescript/ai-client/src/chat-client.ts
      Updated cleanup logic to unconditionally call stop() on unmount; added post-stream action queuing in chat-client for deferred processing.
      Documentation: Guides
      docs/guides/{tree-shaking,text-to-speech,transcription,image-generation,video-generation,runtime-adapter-switching}.md, docs/guides/agentic-cycle.md, docs/getting-started/{overview,quick-start}.md
      Added new guides for tree-shaking, TTS, transcription, image/video generation, and runtime adapter switching; updated existing guides to reflect new adapter naming.
      Documentation: Adapter Pages
      docs/adapters/{anthropic,gemini,ollama,openai}.md
      Updated adapter documentation with new factory names (createAnthropicChat → createAnthropicText, etc.), new adapters (summarize, embedding, image, TTS), and revised examples.
      Documentation: API & Reference
      docs/api/{ai,ai-client,ai-react,ai-solid}.md, docs/reference/{functions,interfaces,type-aliases}/*, docs/config.json
      Renamed API references (ChatOptions → TextOptions, chat → text, etc.); added new guide entries; updated configuration metadata with ordering and new pages.
      Examples (Multi-Framework)
      examples/{ts-react-chat,ts-group-chat,ts-solid-chat,ts-svelte-chat,ts-vue-chat}/*, examples/README.md
      Updated all example files to use new adapter naming (openaiText, anthropicText, etc.), refactored provider selection logic to use typed option factories instead of switch statements.
      Package.json Updates
      Multiple package.json files across packages and examples
      Updated devDependency vite (^7.2.4 → ^7.2.7); added zod to peerDependencies in anthropic/gemini/ollama/openai packages; added new export path "./adapters" in core AI package.
      Removed Legacy Files
      packages/typescript/ai/src/{core/chat-common-options.ts,core/embedding.ts,core/summarize.ts,utilities/chat-options.ts,tools/zod-converter.ts}
      Deleted legacy standalone utilities; functionality moved into activities modules.
      README Updates
      README.md, packages/typescript/ai-solid/README.md
      Updated with tree-shakeable adapters feature highlight and example code reflecting new adapter API.

      Estimated code review effort

      🎯 4 (Complex) | ⏱️ ~60–90 minutes

      Areas requiring extra attention during review:

      • New adapter implementations across all providers (OpenAI, Anthropic, Gemini, Ollama): verify correct client initialization, API call mappings, error handling, and consistent patterns for streaming and structured output.
      • Type system changes: ensure TextOptions and related renamed types are correctly constrained with generics, and that type inference works as intended across adapter/model combinations.
      • Activity orchestration (text/chat, embedding, summarize, etc.): validate event emission ordering, error propagation, state management, and compatibility with client-side hooks (React, Solid, Vue, Svelte).
      • Tool call and structured output handling: review logic for tool routing, partial JSON parsing, and schema validation in the new architecture.
      • Post-stream action queuing in chat-client: confirm deferred processing works correctly when streams are active and doesn't introduce timing-related bugs.
      • Example and documentation consistency: spot-check that all example code and guide snippets correctly reflect the new adapter and option naming conventions.

      🐰 Whiskers twitches with delight at this grand refactor—

      Tree-shaking adapters, bundled tighter, branching bolder,

      Each provider a module, users build what they hold, sir!

      Smaller bytes, type safety, a framework much lighter—

      Hip hop hooray for this architectural exciter! 🌳✨

      Pre-merge checks and finishing touches

      ❌ Failed checks (1 inconclusive)
      Check nameStatusExplanationResolution
      Description check❓ InconclusiveThe description follows the required template with 🎯 Changes and ✅ Checklist sections. However, the Changes section lacks detail about the specific implementation and impact. The Release Impact section has unchecked boxes indicating no changeset was generated despite the question about deprecating the old approach, which suggests release documentation may be incomplete.Complete the description by adding more detail about implementation approach for the generate function, migration strategy, and generate a changeset if this affects published code.
      ✅ Passed checks (2 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly and concisely describes the main changes: splitting adapters into separate functionalities (text, embed, summarize) and adding a generate function.
      Docstring Coverage✅ PassedDocstring coverage is 81.31% which is sufficient. The required threshold is 80.00%.

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands and usage tips.
    4. Actionable comments posted: 16

      Caution

      Some comments are outside the diff and can’t be posted inline due to platform limitations.

      ⚠️ Outside diff range comments (1)
      README.md (1)

      41-73: Fix README example imports to match actual package exports

      The new tree-shakeable adapters section has two import path issues that will prevent the example from compiling:

      1. Function name mismatch: The example imports import { generate } from '@tanstack/ai', but the root package only exports ai (not generate). Should be import { ai } from '@tanstack/ai'.

      2. Adapter path mismatch: The example shows import { openaiText } from '@tanstack/ai-openai/adapters', but adapters are re-exported at the package root. Should be import { openaiText } from '@tanstack/ai-openai'.

      Update the code snippet to use:

      import { openaiText } from '@tanstack/ai-openai'
      import { ai } from '@tanstack/ai'
      ♻️ Duplicate comments (1)
      packages/typescript/ai-openai/src/utils/client.ts (1)

      45-47: Duplicate: generateId duplication.

      This function is identical to implementations in Gemini, Anthropic, and Ollama adapters. See the comment on packages/typescript/ai-gemini/src/utils/client.ts (lines 41-43) for consolidation recommendations.

      🧹 Nitpick comments (25)
      packages/typescript/ai-anthropic/src/utils/client.ts (1)

      43-45: Consider centralizing generateId to eliminate duplication.

      This exact implementation is duplicated across multiple provider utilities (OpenAI, Gemini, Ollama) and the base adapters in @tanstack/ai. Consider extracting it to a shared location in @tanstack/ai and re-exporting from there.

      Additionally, substring(7) may return fewer than 4 characters for small random values. Using substring(2, 9) or padEnd would ensure consistent length.

      -export function generateId(prefix: string): string {
      -  return `${prefix}-${Date.now()}-${Math.random().toString(36).substring(7)}`
      +export function generateId(prefix: string): string {
      +  return `${prefix}-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`
       }
      packages/typescript/ai-anthropic/src/adapters/summarize.ts (3)

      32-34: Redundant kind declaration.

      BaseSummarizeAdapter already sets kind = 'summarize' as const. This redeclaration is unnecessary unless you intend to override with a different value.

       export class AnthropicSummarizeAdapter extends BaseSummarizeAdapter<
         typeof ANTHROPIC_MODELS,
         AnthropicSummarizeProviderOptions
       > {
      -  readonly kind = 'summarize' as const
         readonly name = 'anthropic' as const
         readonly models = ANTHROPIC_MODELS

      71-97: buildSummarizationPrompt is duplicated across providers.

      This method is identical to the implementation in OpenAI.buildSummarizationPrompt (see openai-adapter.ts lines 171-197). Consider extracting it to a shared utility in @tanstack/ai to reduce duplication and ensure consistent behavior across providers.


      38-41: Consider passing config to base class.

      The constructor passes an empty object to super({}) while BaseSummarizeAdapter accepts SummarizeAdapterConfig with fields like timeout, maxRetries, and headers. If AnthropicSummarizeConfig expands to include these fields, they won't propagate to the base class.

       constructor(config: AnthropicSummarizeConfig) {
      -  super({})
      +  super(config)
         this.client = createAnthropicClient(config)
       }
      packages/typescript/ai/src/adapters/base-chat-adapter.ts (1)

      11-121: Solid base chat adapter design; consider wiring TModels into ChatOptions for extra safety

      The ChatAdapterConfig, ChatAdapter generics, and BaseChatAdapter shape look coherent and extensible: the kind: 'chat' discriminator, type‑only _providerOptions/modalities/metadata fields, and chatStream contract are all aligned with how a unified generate/ai API will discriminate adapters.

      If you ever want stricter coupling between supported models and chatStream calls, you could consider tightening the signature to something like:

      chatStream: (
        options: ChatOptions<TModels[number], TProviderOptions>,
      ) => AsyncIterable<StreamChunk>

      so options.model can’t drift from models. Not required, but it’s a low‑cost type‑safety win if it doesn’t fight ChatOptions’ current generic constraints.

      packages/typescript/ai/src/adapters/index.ts (1)

      1-33: Adapter index looks good; consider deriving AdapterKind from AnyAdapter

      The base adapter re-exports and AnyAdapter union are a nice central place to hang adapter‑level types.

      To reduce the chance of drift if you add another adapter kind later, you could derive AdapterKind from AnyAdapter instead of hard‑coding the union, e.g.:

      export type AnyAdapter =
        | ChatAdapter<any, any, any, any, any>
        | EmbeddingAdapter<any, any>
        | SummarizeAdapter<any, any>
      
      export type AdapterKind = AnyAdapter['kind']

      Not required, but it keeps the discriminator and the union in sync automatically.

      packages/typescript/ai-gemini/src/utils/client.ts (1)

      41-43: Consider consolidating generateId across adapter packages.

      This identical function is duplicated across Gemini, OpenAI, Anthropic, Ollama, and base adapter classes. Consider extracting it to a shared utility module (e.g., @tanstack/ai/utils) to reduce duplication and ease maintenance.

      Example consolidation in packages/typescript/ai/src/utils/id.ts:

      export function generateId(prefix: string): string {
        return `${prefix}-${Date.now()}-${Math.random().toString(36).substring(7)}`
      }

      Then import from the shared location in each adapter package.

      packages/typescript/ai-ollama/src/utils/client.ts (1)

      39-41: Consider removing the default parameter for consistency.

      This generateId implementation has a default parameter 'msg', while the identical function in OpenAI, Anthropic, and Gemini adapters doesn't have a default. For consistency across the codebase, consider removing the default or applying it uniformly.

      Note: This also relates to the code duplication mentioned in the Gemini utils review—consolidating generateId would naturally resolve this inconsistency.

      packages/typescript/ai-ollama/src/adapters/text.ts (2)

      254-260: Duplicate code in image source handling.

      Both branches of the conditional (lines 255-259) execute identical code. The distinction between data and URL sources appears intended but isn't implemented.

      Simplify by removing the redundant conditional:

                 } else if (part.type === 'image') {
      -            if (part.source.type === 'data') {
      -              images.push(part.source.value)
      -            } else {
      -              images.push(part.source.value)
      -            }
      +            images.push(part.source.value)
                 }

      Or, if different handling was intended (e.g., prefixing data URIs), implement the distinction.


      280-292: Silent failure on JSON parse error.

      The empty catch block silently converts invalid JSON arguments to an empty object, which could mask malformed tool call data and cause downstream failures.

      Consider logging or preserving the original string:

                         try {
                           parsedArguments = JSON.parse(
                             toolCall.function.arguments,
                           ) as Record<string, unknown>
                         } catch {
      -                    parsedArguments = {}
      +                    // Log or handle malformed arguments
      +                    console.warn(`Failed to parse tool call arguments for ${toolCall.function.name}`)
      +                    parsedArguments = { _raw: toolCall.function.arguments }
                         }
      packages/typescript/ai-gemini/src/adapters/embed.ts (2)

      74-86: Sequential API calls for multiple inputs may be inefficient.

      Each input triggers a separate API call in sequence. For batches of inputs, this adds latency. Consider parallelizing with Promise.all if the Gemini API supports concurrent requests, or check if batch embedding is available.

      -    for (const input of inputs) {
      -      const response = await this.client.models.embedContent({
      +    const responses = await Promise.all(
      +      inputs.map((input) =>
      +        this.client.models.embedContent({
      -        model,
      -        contents: [{ role: 'user', parts: [{ text: input }] }],
      -        config: {
      -          outputDimensionality: options.dimensions,
      -        },
      -      })
      +          model,
      +          contents: [{ role: 'user', parts: [{ text: input }] }],
      +          config: {
      +            outputDimensionality: options.dimensions,
      +          },
      +        }),
      +      ),
      +    )
       
      -      if (response.embeddings?.[0]?.values) {
      -        embeddings.push(response.embeddings[0].values)
      +    for (const response of responses) {
      +      if (response.embeddings?.[0]?.values) {
      +        embeddings.push(response.embeddings[0].values)
             }
           }

      88-96: Hardcoded usage tokens and inconsistent ID generation.

      Usage tokens are hardcoded to 0, which doesn't reflect actual API consumption. Also, Date.now() is used directly instead of the generateId utility used in other adapters (e.g., Ollama summarize adapter uses generateId('sum')).

      For consistency with other adapters:

      +import { generateId } from '../utils'
      +
           return {
      -      id: `embed-${Date.now()}`,
      +      id: generateId('embed'),
             model,
             embeddings,
             usage: {
      -        promptTokens: 0,
      -        totalTokens: 0,
      +        promptTokens: 0, // TODO: Gemini API may provide token counts in response
      +        totalTokens: 0,
             },
           }
      packages/typescript/ai-openai/src/adapters/summarize.ts (1)

      15-20: Provider options are defined but never used.

      OpenAISummarizeProviderOptions defines temperature and maxTokens, but the summarize() method hardcodes temperature: 0.3 (line 53) and uses options.maxLength for max_tokens. Consider either removing these unused options or applying them in the API call.

      packages/typescript/ai-gemini/src/adapters/summarize.ts (2)

      50-53: Inconsistent inheritance pattern.

      This adapter implements SummarizeAdapter directly, while OpenAISummarizeAdapter extends BaseSummarizeAdapter. For consistency and to leverage shared functionality (like the base generateId), consider extending BaseSummarizeAdapter instead.


      26-40: Provider options are defined but not applied.

      GeminiSummarizeProviderOptions defines generationConfig and safetySettings, but these are not passed to the generateContent call. Either use these options in the API request or remove them from the interface.

           const response = await this.client.models.generateContent({
             model,
             contents: [
               {
                 role: 'user',
                 parts: [
                   { text: `Please summarize the following:\n\n${options.text}` },
                 ],
               },
             ],
             config: {
               systemInstruction: systemPrompt,
      +        ...options.providerOptions?.generationConfig,
             },
      +      safetySettings: options.providerOptions?.safetySettings,
           })
      packages/typescript/ai-openai/src/adapters/text.ts (3)

      100-107: Remove or replace debug logging.

      These console.error statements appear to be debugging artifacts. Consider removing them or using a proper logging abstraction that can be configured for different environments.

           } catch (error: unknown) {
             const err = error as Error
      -      console.error('>>> chatStream: Fatal error during response creation <<<')
      -      console.error('>>> Error message:', err.message)
      -      console.error('>>> Error stack:', err.stack)
      -      console.error('>>> Full error:', err)
             throw error
           }

      380-400: Debug logging in catch block.

      Similar to the earlier comment, the console.log should be removed or replaced with a configurable logging mechanism. Also note the unused eventTypeCounts referenced here.


      348-351: Consider a more robust type check.

      The type assertion (item as { type: string }).type works but is fragile. Consider using a type guard or checking for the property existence first.

      -          const hasFunctionCalls = chunk.response.output.some(
      -            (item: unknown) =>
      -              (item as { type: string }).type === 'function_call',
      -          )
      +          const hasFunctionCalls = chunk.response.output.some(
      +            (item) =>
      +              item && typeof item === 'object' && 'type' in item && item.type === 'function_call',
      +          )
      packages/typescript/ai-ollama/src/adapters/embed.ts (2)

      85-92: Sequential API calls may impact performance for batch embeddings.

      Each input triggers a separate client.embeddings() call. If the Ollama API supports batching multiple prompts in a single request, consider leveraging that to reduce latency and overhead. Otherwise, for large input arrays, consider using Promise.all for concurrent requests (with appropriate rate limiting if needed).


      100-100: Non-unique ID generation.

      Date.now() can produce duplicate IDs for rapid successive calls. Consider using a UUID library or combining with a random suffix:

      -      id: `embed-${Date.now()}`,
      +      id: `embed-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`,
      packages/typescript/ai/src/types.ts (1)

      994-1037: Duplication with ChatStreamOptionsForModel.

      ChatOptionsForChatAdapter (lines 994-1037) is structurally very similar to ChatStreamOptionsForModel (lines 887-937). The main difference is the adapter base type (ChatAdapter vs AIAdapter). Consider whether these could share a common base type or be unified to reduce maintenance overhead as the API evolves.

      packages/typescript/ai-gemini/src/adapters/text.ts (1)

      157-162: Silent error swallowing in argument merging.

      The catch block silently replaces the existing args with the new args when JSON parsing fails, which could mask data corruption issues. Consider logging a warning:

                   } catch {
      +              // Log or track parse failure for debugging
                     toolCallData.args =
                       typeof functionArgs === 'string'
                         ? functionArgs
                         : JSON.stringify(functionArgs)
                   }
      packages/typescript/ai/src/core/generate.ts (3)

      495-497: Dead code - outputSchema is guaranteed to be defined.

      This function is only called when chatOptions.outputSchema is truthy (line 347), and outputSchema is destructured from options. This check will never trigger.

      -  // Validate against the schema
      -  if (!outputSchema) {
      -    throw new Error('outputSchema is required for structured output')
      -  }
      -
         const result = outputSchema.safeParse(parsed)

      223-224: Missing newline between type definitions.

      There's a missing newline separator before the GenerateReturnType comment, which reduces readability:

      -      : never /** Infer the return type based on adapter kind, schema, and stream */
      -type GenerateReturnType<
      +      : never
      +
      +/** Infer the return type based on adapter kind, schema, and stream */
      +type GenerateReturnType<

      550-589: generateSummaryStream doesn't actually stream.

      This function awaits the full summarize() result before yielding any chunks, so it provides a streaming interface but not streaming behavior. This may be intentional for API consistency, but consider documenting this limitation or implementing true streaming if the underlying adapter supports it.

      📜 Review details

      Configuration used: CodeRabbit UI

      Review profile: CHILL

      Plan: Pro

      📥 Commits

      Reviewing files that changed from the base of the PR and between 049eb8acd83e6d566c6040c0c4cb53dbe222d46a and 86fa305bf3a71f1f1711f656d5133bfc4e5d4df3.

      📒 Files selected for processing (40)
      • .changeset/two-bikes-kneel.md (1 hunks)
      • README.md (2 hunks)
      • docs/guides/tree-shakeable-adapters.md (1 hunks)
      • examples/ts-react-chat/src/routes/api.tanchat.ts (2 hunks)
      • packages/typescript/ai-anthropic/src/adapters/summarize.ts (1 hunks)
      • packages/typescript/ai-anthropic/src/adapters/text.ts (1 hunks)
      • packages/typescript/ai-anthropic/src/index.ts (1 hunks)
      • packages/typescript/ai-anthropic/src/utils/client.ts (1 hunks)
      • packages/typescript/ai-anthropic/src/utils/index.ts (1 hunks)
      • packages/typescript/ai-gemini/src/adapters/embed.ts (1 hunks)
      • packages/typescript/ai-gemini/src/adapters/summarize.ts (1 hunks)
      • packages/typescript/ai-gemini/src/adapters/text.ts (1 hunks)
      • packages/typescript/ai-gemini/src/index.ts (1 hunks)
      • packages/typescript/ai-gemini/src/model-meta.ts (1 hunks)
      • packages/typescript/ai-gemini/src/utils/client.ts (1 hunks)
      • packages/typescript/ai-gemini/src/utils/index.ts (1 hunks)
      • packages/typescript/ai-ollama/src/adapters/embed.ts (1 hunks)
      • packages/typescript/ai-ollama/src/adapters/summarize.ts (1 hunks)
      • packages/typescript/ai-ollama/src/adapters/text.ts (1 hunks)
      • packages/typescript/ai-ollama/src/index.ts (1 hunks)
      • packages/typescript/ai-ollama/src/utils/client.ts (1 hunks)
      • packages/typescript/ai-ollama/src/utils/index.ts (1 hunks)
      • packages/typescript/ai-openai/src/adapters/embed.ts (1 hunks)
      • packages/typescript/ai-openai/src/adapters/summarize.ts (1 hunks)
      • packages/typescript/ai-openai/src/adapters/text.ts (1 hunks)
      • packages/typescript/ai-openai/src/index.ts (1 hunks)
      • packages/typescript/ai-openai/src/openai-adapter.ts (1 hunks)
      • packages/typescript/ai-openai/src/utils/client.ts (1 hunks)
      • packages/typescript/ai-openai/src/utils/index.ts (1 hunks)
      • packages/typescript/ai/package.json (1 hunks)
      • packages/typescript/ai/src/adapters/base-chat-adapter.ts (1 hunks)
      • packages/typescript/ai/src/adapters/base-embedding-adapter.ts (1 hunks)
      • packages/typescript/ai/src/adapters/base-summarize-adapter.ts (1 hunks)
      • packages/typescript/ai/src/adapters/index.ts (1 hunks)
      • packages/typescript/ai/src/core/generate.ts (1 hunks)
      • packages/typescript/ai/src/index.ts (1 hunks)
      • packages/typescript/ai/src/types.ts (2 hunks)
      • packages/typescript/ai/tests/generate-types.test-d.ts (1 hunks)
      • packages/typescript/ai/tests/generate.test.ts (1 hunks)
      • packages/typescript/ai/vite.config.ts (1 hunks)
      🧰 Additional context used
      🧬 Code graph analysis (16)
      packages/typescript/ai-gemini/src/model-meta.ts (1)
      packages/typescript/ai-gemini/src/index.ts (2)
      • GeminiModels (38-38)
      • GEMINI_MODELS (37-37)
      packages/typescript/ai/src/adapters/index.ts (3)
      packages/typescript/ai/src/adapters/base-chat-adapter.ts (1)
      • ChatAdapter (30-70)
      packages/typescript/ai/src/adapters/base-embedding-adapter.ts (1)
      • EmbeddingAdapter (22-41)
      packages/typescript/ai/src/adapters/base-summarize-adapter.ts (1)
      • SummarizeAdapter (22-41)
      packages/typescript/ai/src/adapters/base-summarize-adapter.ts (1)
      packages/typescript/ai/src/types.ts (2)
      • SummarizationOptions (703-709)
      • SummarizationResult (711-720)
      packages/typescript/ai/tests/generate-types.test-d.ts (3)
      packages/typescript/ai/src/adapters/index.ts (3)
      • BaseChatAdapter (7-7)
      • BaseEmbeddingAdapter (14-14)
      • BaseSummarizeAdapter (21-21)
      packages/typescript/ai/src/types.ts (6)
      • ChatOptions (554-588)
      • StreamChunk (678-686)
      • EmbeddingOptions (722-726)
      • EmbeddingResult (728-736)
      • SummarizationOptions (703-709)
      • SummarizationResult (711-720)
      packages/typescript/ai/src/core/generate.ts (1)
      • ai (315-393)
      packages/typescript/ai-gemini/src/utils/client.ts (7)
      packages/typescript/ai-gemini/src/utils/index.ts (4)
      • GeminiClientConfig (5-5)
      • createGeminiClient (2-2)
      • getGeminiApiKeyFromEnv (4-4)
      • generateId (3-3)
      packages/typescript/ai/src/adapters/base-chat-adapter.ts (1)
      • generateId (119-121)
      packages/typescript/ai/src/adapters/base-embedding-adapter.ts (1)
      • generateId (66-68)
      packages/typescript/ai/src/adapters/base-summarize-adapter.ts (1)
      • generateId (68-70)
      packages/typescript/ai-anthropic/src/utils/client.ts (1)
      • generateId (43-45)
      packages/typescript/ai-ollama/src/utils/client.ts (1)
      • generateId (39-41)
      packages/typescript/ai-openai/src/utils/client.ts (1)
      • generateId (45-47)
      packages/typescript/ai-anthropic/src/utils/client.ts (6)
      packages/typescript/ai/src/adapters/base-chat-adapter.ts (1)
      • generateId (119-121)
      packages/typescript/ai/src/adapters/base-embedding-adapter.ts (1)
      • generateId (66-68)
      packages/typescript/ai/src/adapters/base-summarize-adapter.ts (1)
      • generateId (68-70)
      packages/typescript/ai-gemini/src/utils/client.ts (1)
      • generateId (41-43)
      packages/typescript/ai-ollama/src/utils/client.ts (1)
      • generateId (39-41)
      packages/typescript/ai-openai/src/utils/client.ts (1)
      • generateId (45-47)
      packages/typescript/ai-openai/src/adapters/embed.ts (6)
      packages/typescript/ai-openai/src/utils/client.ts (4)
      • OpenAIClientConfig (3-7)
      • createOpenAIClient (12-18)
      • generateId (45-47)
      • getOpenAIApiKeyFromEnv (24-40)
      packages/typescript/ai-openai/src/model-meta.ts (1)
      • OPENAI_EMBEDDING_MODELS (1664-1668)
      packages/typescript/ai/src/types.ts (2)
      • EmbeddingOptions (722-726)
      • EmbeddingResult (728-736)
      packages/typescript/ai/src/adapters/base-chat-adapter.ts (1)
      • generateId (119-121)
      packages/typescript/ai/src/adapters/base-embedding-adapter.ts (1)
      • generateId (66-68)
      packages/typescript/ai/src/adapters/base-summarize-adapter.ts (1)
      • generateId (68-70)
      packages/typescript/ai-openai/src/openai-adapter.ts (2)
      packages/typescript/ai-anthropic/src/text/text-provider-options.ts (1)
      • validateTextProviderOptions (198-204)
      packages/typescript/ai-openai/src/text/text-provider-options.ts (1)
      • validateTextProviderOptions (320-325)
      packages/typescript/ai-ollama/src/adapters/text.ts (4)
      packages/typescript/ai-ollama/src/index.ts (5)
      • OllamaTextModels (8-8)
      • OllamaTextModel (12-12)
      • OllamaTextProviderOptions (13-13)
      • OllamaTextAdapter (7-7)
      • Ollama (49-49)
      packages/typescript/ai-ollama/src/utils/index.ts (3)
      • createOllamaClient (2-2)
      • generateId (4-4)
      • getOllamaHostFromEnv (5-5)
      packages/typescript/ai/src/types.ts (3)
      • StreamChunk (678-686)
      • ToolCall (68-75)
      • Tool (326-427)
      packages/typescript/ai/src/adapters/base-chat-adapter.ts (1)
      • generateId (119-121)
      packages/typescript/ai-gemini/src/adapters/summarize.ts (4)
      packages/typescript/ai/src/adapters/base-summarize-adapter.ts (2)
      • SummarizeAdapter (22-41)
      • generateId (68-70)
      packages/typescript/ai/src/types.ts (3)
      • SummarizeAdapter (1040-1040)
      • SummarizationOptions (703-709)
      • SummarizationResult (711-720)
      packages/typescript/ai-gemini/src/utils/client.ts (3)
      • createGeminiClient (10-14)
      • generateId (41-43)
      • getGeminiApiKeyFromEnv (20-36)
      packages/typescript/ai-gemini/src/utils/index.ts (3)
      • createGeminiClient (2-2)
      • generateId (3-3)
      • getGeminiApiKeyFromEnv (4-4)
      packages/typescript/ai-openai/src/utils/client.ts (6)
      packages/typescript/ai/src/adapters/base-chat-adapter.ts (1)
      • generateId (119-121)
      packages/typescript/ai/src/adapters/base-embedding-adapter.ts (1)
      • generateId (66-68)
      packages/typescript/ai/src/adapters/base-summarize-adapter.ts (1)
      • generateId (68-70)
      packages/typescript/ai-anthropic/src/utils/client.ts (1)
      • generateId (43-45)
      packages/typescript/ai-gemini/src/utils/client.ts (1)
      • generateId (41-43)
      packages/typescript/ai-ollama/src/utils/client.ts (1)
      • generateId (39-41)
      packages/typescript/ai-ollama/src/utils/client.ts (3)
      packages/typescript/ai-anthropic/src/utils/client.ts (1)
      • generateId (43-45)
      packages/typescript/ai-gemini/src/utils/client.ts (1)
      • generateId (41-43)
      packages/typescript/ai-openai/src/utils/client.ts (1)
      • generateId (45-47)
      packages/typescript/ai/src/adapters/base-embedding-adapter.ts (2)
      packages/typescript/ai/src/adapters/index.ts (3)
      • EmbeddingAdapterConfig (16-16)
      • EmbeddingAdapter (15-15)
      • BaseEmbeddingAdapter (14-14)
      packages/typescript/ai/src/types.ts (3)
      • EmbeddingAdapter (1040-1040)
      • EmbeddingOptions (722-726)
      • EmbeddingResult (728-736)
      packages/typescript/ai-anthropic/src/adapters/summarize.ts (4)
      packages/typescript/ai-anthropic/src/utils/client.ts (3)
      • AnthropicClientConfig (3-5)
      • createAnthropicClient (10-16)
      • getAnthropicApiKeyFromEnv (22-38)
      packages/typescript/ai-anthropic/src/utils/index.ts (3)
      • AnthropicClientConfig (5-5)
      • createAnthropicClient (2-2)
      • getAnthropicApiKeyFromEnv (4-4)
      packages/typescript/ai-anthropic/src/model-meta.ts (1)
      • ANTHROPIC_MODELS (363-373)
      packages/typescript/ai/src/types.ts (2)
      • SummarizationOptions (703-709)
      • SummarizationResult (711-720)
      packages/typescript/ai/src/types.ts (4)
      packages/typescript/ai/src/adapters/base-chat-adapter.ts (1)
      • ChatAdapter (30-70)
      packages/typescript/ai/src/adapters/index.ts (3)
      • ChatAdapter (8-8)
      • EmbeddingAdapter (15-15)
      • SummarizeAdapter (22-22)
      packages/typescript/ai/src/adapters/base-embedding-adapter.ts (1)
      • EmbeddingAdapter (22-41)
      packages/typescript/ai/src/adapters/base-summarize-adapter.ts (1)
      • SummarizeAdapter (22-41)
      packages/typescript/ai-openai/src/adapters/text.ts (1)
      packages/typescript/ai-openai/src/utils/index.ts (4)
      • OpenAIClientConfig (5-5)
      • createOpenAIClient (2-2)
      • generateId (4-4)
      • getOpenAIApiKeyFromEnv (3-3)
      🪛 LanguageTool
      docs/guides/tree-shakeable-adapters.md

      [style] ~173-~173: This phrase is redundant. Consider writing “eliminated”. Context: ... your bundle - Unused adapter types are completely eliminated - Smaller bundles lead to faster load t...

      (COMPLETELY_ANNIHILATE)

      ⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
      • GitHub Check: Test
      🔇 Additional comments (53)
      packages/typescript/ai/package.json (1)

      20-23: No changes needed. The "./adapters" export path correctly re-exports all three adapter types (chat, embedding, summarize) through named exports in adapters/index.ts, enabling proper tree-shaking. The structure follows the established pattern and does not require separate sub-export paths.

      Likely an incorrect or invalid review comment.

      packages/typescript/ai/vite.config.ts (1)

      32-36: New adapter entry point correctly configured for tree-shaking.

      The addition of './src/adapters/index.ts' as a separate entry point is properly set up. The file exists and exports the three adapter types (chat, embedding, summarize) as intended. The package.json exports field correctly exposes the adapters entry point at "./adapters", enabling independent imports and tree-shaking of adapter functionality.

      packages/typescript/ai-openai/src/openai-adapter.ts (1)

      508-512: LGTM - validation now includes model context.

      The expansion of validateTextProviderOptions to include model: options.model alongside input and providerOptions is appropriate. This enables model-specific validation rules (e.g., checking if certain provider options are compatible with the selected model).

      packages/typescript/ai-openai/src/utils/index.ts (1)

      1-6: LGTM - clean barrel export for OpenAI utilities.

      The re-export pattern is appropriate for consolidating client utilities and enabling tree-shakeable imports.

      packages/typescript/ai-anthropic/src/utils/index.ts (1)

      1-6: LGTM - consistent with OpenAI utility barrel pattern.

      The re-export structure mirrors the OpenAI package, maintaining consistency across provider adapters.

      packages/typescript/ai-anthropic/src/utils/client.ts (2)

      22-38: LGTM - environment detection for API key retrieval.

      The fallback chain from globalThis.window.env to process.env handles both browser and Node.js environments appropriately. The error message is clear and actionable.


      3-16: LGTM - clean client factory implementation.

      The AnthropicClientConfig interface and createAnthropicClient factory follow the established pattern from other provider adapters.

      packages/typescript/ai/src/adapters/base-summarize-adapter.ts (2)

      22-41: LGTM - well-designed adapter interface with discriminator.

      The kind: 'summarize' discriminator enables type-safe routing in the generate() function. The generic parameters for TModels and TProviderOptions provide good type inference for concrete implementations.


      47-71: LGTM - abstract base class follows established adapter patterns.

      The use of declare for the type-only _providerOptions is correct. The structure mirrors BaseChatAdapter and BaseEmbeddingAdapter, maintaining consistency across adapter types.

      packages/typescript/ai-anthropic/src/adapters/summarize.ts (1)

      103-118: LGTM - factory functions follow established patterns.

      The createAnthropicSummarize and anthropicSummarize factories mirror the conventions used in other provider adapters, providing both explicit and environment-based API key options.

      .changeset/two-bikes-kneel.md (1)

      1-9: Changeset scope and message look appropriate

      Minor bumps for the affected @tanstack/ai-* packages and the short description accurately reflect the adapter split for tree‑shaking. No adjustments needed.

      packages/typescript/ai-gemini/src/model-meta.ts (1)

      771-783: GeminiModels alias correctly captures the model name union

      GeminiModels = (typeof GEMINI_MODELS)[number] is the right way to derive the string‑literal union from the model list and lines up with the GeminiTextModel re‑export pattern in index.ts. This should improve ergonomics without changing behavior.

      packages/typescript/ai-ollama/src/utils/index.ts (1)

      1-7: Ollama utils barrel export is clear and consistent

      The utils index cleanly re-exports the client helpers and config type from ./client, matching the pattern used in other provider packages and making the public surface easier to consume. Looks good as-is.

      packages/typescript/ai/src/index.ts (1)

      4-12: No action required. The review comment's premise is incorrect.

      The codebase consistently exports and documents the ai function. All JSDoc examples in core/generate.ts use import { ai }, and the root index.ts correctly exports ai from ./core/generate. There is no documentation (README or guide) claiming users should import generate—no such guide exists in the package, and the README contains no code examples. The API is already consistent.

      Likely an incorrect or invalid review comment.

      packages/typescript/ai-gemini/src/utils/index.ts (1)

      1-6: LGTM!

      Clean barrel export consolidating Gemini utilities for easy import.

      examples/ts-react-chat/src/routes/api.tanchat.ts (1)

      2-3: LGTM!

      Import changes align with the new tree-shakeable adapter pattern.

      packages/typescript/ai-gemini/src/utils/client.ts (3)

      1-5: LGTM!

      Clean interface definition for Gemini client configuration.


      10-14: LGTM!

      Clean factory function for creating Gemini client instances.


      20-36: LGTM!

      Good environment-aware API key retrieval with clear error messaging and support for both GOOGLE_API_KEY and GEMINI_API_KEY.

      packages/typescript/ai/tests/generate.test.ts (1)

      1-287: LGTM!

      Comprehensive test suite covering all three adapter types (chat, embedding, summarize) with proper mocking and type safety checks. Well-structured and thorough.

      packages/typescript/ai-ollama/src/utils/client.ts (4)

      1-5: LGTM!

      Clean configuration interface for Ollama client.


      10-14: LGTM!

      Good default host configuration for local Ollama instances.


      20-34: LGTM!

      Good environment-aware host resolution with sensible localhost fallback.


      46-49: LGTM!

      Reasonable token estimation heuristic for Ollama. The approximation is clearly documented in the comment.

      packages/typescript/ai-openai/src/adapters/embed.ts (5)

      1-27: LGTM!

      Well-documented interfaces with clear API reference. Good use of base adapter patterns.


      35-48: LGTM!

      Clean adapter class structure following the base adapter pattern with proper typing.


      50-66: LGTM!

      Clean implementation of the embeddings method with proper response mapping and sensible model fallback.


      81-86: LGTM!

      Well-documented factory function with clear example usage.


      111-116: LGTM!

      Well-documented factory with automatic environment-based API key detection. The example clearly shows the intended usage pattern.

      packages/typescript/ai-openai/src/utils/client.ts (3)

      1-7: LGTM!

      Clean configuration interface with appropriate optional fields for OpenAI SDK configuration.


      12-18: LGTM!

      Clean factory function properly initializing the OpenAI SDK client.


      24-40: LGTM!

      Good environment-aware API key retrieval with clear error messaging.

      packages/typescript/ai/tests/generate-types.test-d.ts (1)

      1-529: LGTM!

      Excellent comprehensive type-level test suite. The tests thoroughly cover:

      • Return type inference for all adapter types
      • Model validation and constraints
      • Strict providerOptions typing
      • outputSchema handling with complex nested types
      • Summarize streaming behavior

      The use of @ts-expect-error for negative test cases is appropriate and ensures type safety. Well-structured and thorough.

      packages/typescript/ai-ollama/src/index.ts (1)

      1-53: Well-structured tree-shakeable exports with proper deprecation notice.

      The organization is clear with new adapters at the top and deprecated legacy exports clearly marked. The deprecation JSDoc provides helpful migration guidance.

      packages/typescript/ai-anthropic/src/index.ts (1)

      1-63: Clean export structure with appropriate provider-specific note.

      The note on line 23 about Anthropic not supporting embeddings natively is helpful for users. The deprecation pattern is consistent with other adapters.

      packages/typescript/ai-ollama/src/adapters/summarize.ts (2)

      64-88: Adapter class structure looks good.

      The class properly implements the SummarizeAdapter interface with appropriate readonly properties and flexible constructor accepting either host string or client instance.


      152-167: Factory functions follow the established pattern.

      createOllamaSummarize for explicit host and ollamaSummarize for environment-based configuration aligns with other adapters in the codebase.

      packages/typescript/ai-ollama/src/adapters/text.ts (2)

      125-132: Streaming implementation looks correct.

      The chatStream method properly delegates to the Ollama client with stream: true and processes chunks via the generator.


      309-328: Options mapping is clean.

      Common options are correctly mapped to Ollama's expected format, with provider-specific options spread appropriately.

      packages/typescript/ai-gemini/src/adapters/embed.ts (1)

      39-62: Adapter class structure follows the established pattern.

      The class properly implements EmbeddingAdapter with readonly properties and flexible constructor accepting either API key or client instance.

      packages/typescript/ai/src/adapters/base-embedding-adapter.ts (1)

      1-69: LGTM! Clean base abstraction for embedding adapters.

      The interface and abstract class provide a well-structured foundation for provider-specific embedding adapters. The use of declare for _providerOptions correctly marks it as a type-only property without runtime overhead.

      packages/typescript/ai-openai/src/adapters/summarize.ts (2)

      90-92: Clarify maxLength semantics.

      maxLength is used both as the API's max_tokens parameter (line 52) and in the prompt as a hint. The prompt says "tokens" but users may expect word or character counts. Consider clarifying the expected unit in the documentation or interface definition.


      43-67: Good defensive handling of API response.

      The optional chaining on response.choices[0]?.message.content and fallback values for usage tokens handle potential undefined responses gracefully.

      packages/typescript/ai-openai/src/index.ts (1)

      1-62: Well-organized export structure with clear deprecation path.

      The separation between new tree-shakeable adapters and deprecated legacy exports is clear. The deprecation notice with migration guidance is helpful for consumers.

      packages/typescript/ai-gemini/src/adapters/summarize.ts (1)

      75-115: Summarize method implementation is correct.

      The method properly builds the prompt, calls the Gemini API, extracts usage metadata, and returns a well-formed SummarizationResult.

      packages/typescript/ai-openai/src/adapters/text.ts (2)

      110-401: Comprehensive streaming implementation.

      The stream processing handles various OpenAI response event types correctly, including content deltas, reasoning, tool calls, and errors. The deduplication logic using hasStreamedContentDeltas and hasStreamedReasoningDeltas flags appropriately prevents duplicate content emission.


      450-537: Message conversion logic is well-structured.

      The convertMessagesToInput method correctly handles different message roles (tool, assistant, user) and converts them to OpenAI's expected format. The tool call serialization and multimodal content handling are properly implemented.

      packages/typescript/ai-ollama/src/adapters/embed.ts (1)

      63-73: LGTM!

      The constructor correctly handles both string host and pre-configured client patterns, providing flexibility for different use cases.

      packages/typescript/ai-gemini/src/index.ts (1)

      1-51: LGTM!

      Clean organization with clear separation between new tree-shakeable adapters and deprecated legacy exports. The deprecation JSDoc provides helpful migration guidance to users.

      packages/typescript/ai/src/types.ts (1)

      970-989: LGTM!

      Well-structured model extraction utilities that correctly infer model unions from each adapter kind, with a unified ExtractModelsFromAnyAdapter for polymorphic usage.

      packages/typescript/ai-gemini/src/adapters/text.ts (1)

      74-99: LGTM!

      Good error handling pattern that catches exceptions and yields a structured error chunk with appropriate metadata.

      packages/typescript/ai/src/core/generate.ts (1)

      305-393: LGTM!

      Well-designed unified ai() function with clean discriminated union routing. The type inference through generics ensures compile-time safety while the runtime switch handles dispatch correctly.

      packages/typescript/ai-anthropic/src/adapters/text.ts (1)

      520-528: The implementation of getAnthropicApiKeyFromEnv() already handles this correctly. The function explicitly throws a clear error with actionable guidance ("ANTHROPIC_API_KEY is required. Please set it in your environment variables or use the factory function with an explicit API key.") when the environment variable is missing, rather than passing undefined to the adapter. The JSDoc also documents this behavior with @throws. No changes needed.

      docs/guides/tree-shakeable-adapters.md lines 1 to 209OutdatedResolvedby AlemTuzlak
      @@ -0,0 +410,21 @@
      410+ }
      411+ }
      412+ } else if (event.type === 'content_block_stop') {
      413+ const existing = toolCallsMap.get(currentToolIndex)
      414+ if (existing && existing.input === '') {
      415+ yield {
      416+ type: 'tool_call',
      417+ id: genId(),
      418+ model: model,
      419+ timestamp,
      420+ toolCall: {
      421+ id: existing.id,
      422+ type: 'function',
      423+ function: {
      424+ name: existing.name,
      425+ arguments: '{}',
      426+ },
      427+ },
      428+ index: currentToolIndex,
      429+ }
      430+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🔴 Critical

      🧩 Analysis chain

      🏁 Script executed:

      cat -n packages/typescript/ai-anthropic/src/adapters/text.ts | sed -n '339,507p'

      Repository: TanStack/ai

      Length of output: 6643


      🏁 Script executed:

      # Search for content_block_stop event type definition
      rg -n 'BetaRawMessageStreamEvent|content_block_stop' --type=ts -A3 -B1 | head -50

      Repository: TanStack/ai

      Length of output: 2122


      🏁 Script executed:

      # Check if there's any Anthropic SDK documentation or types
      find . -name "*.d.ts" -o -name "*.ts" | xargs grep -l 'content_block_stop' 2>/dev/null | head -5

      Repository: TanStack/ai

      Length of output: 170


      🏁 Script executed:

      # Look for Anthropic SDK or documentation files
      fd -e md -e json -e txt | xargs grep -l 'content_block_stop' 2>/dev/null | head -10

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      cat -n packages/typescript/ai-anthropic/src/anthropic-adapter.ts | sed -n '503,530p'

      Repository: TanStack/ai

      Length of output: 1238


      🏁 Script executed:

      # Check the event structure by looking at how content_block_start and content_block_stop are used
      rg -n 'event\.(index|content_block)' packages/typescript/ai-anthropic/src/ --type=ts -B2 -A2

      Repository: TanStack/ai

      Length of output: 2500


      🏁 Script executed:

      # Search for node_modules or type definitions
      find . -path "*/node_modules/@anthropic-ai/sdk/*" -name "*.d.ts" 2>/dev/null | head -3

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      # Alternative: Look at package.json to see Anthropic SDK version
      cat packages/typescript/ai-anthropic/package.json | grep -A2 -B2 anthropic

      Repository: TanStack/ai

      Length of output: 483


      🏁 Script executed:

      # Search for any documentation or comments about content_block_stop ordering
      rg -i 'content_block.*order|block.*index' --type=ts --type=md -C3

      Repository: TanStack/ai

      Length of output: 37


      🌐 Web query:

      Anthropic API content_block_stop event structure BetaRawMessageStreamEvent

      💡 Result:

      The content_block_stop streaming event is sent when a content block finishes. Its payload contains the event type and the content-block index. Example properties:

      • type: "content_block_stop"
      • index: integer (the content block's index in the final Message.content array).

      Sources: Anthropic Streaming docs (event flow + content-block semantics). [1] Rust SDK StreamEvent enum showing ContentBlockStop { index: u32 }. [2]

      References [1] Anthropic — Streaming messages / Event types. [2] anthropic-api Rust docs — StreamEvent enum (ContentBlockStop).


      🏁 Script executed:

      cat -n packages/typescript/ai-anthropic/src/adapters/text.ts | sed -n '412,430p'

      Repository: TanStack/ai

      Length of output: 787


      🏁 Script executed:

      # Check if event.index is used anywhere in the content_block_stop handler
      rg -n 'content_block_stop' -A15 packages/typescript/ai-anthropic/src/adapters/text.ts | grep -E 'index|event\.'

      Repository: TanStack/ai

      Length of output: 116


      Use the index field from content_block_stop to identify which block actually stopped.

      The handler at line 413 checks toolCallsMap.get(currentToolIndex) without verifying that the stopped block is actually a tool_use. Since content_block_stop events include an index field identifying which content block stopped, and currentToolIndex only increments for tool_use blocks, mismatches occur with mixed content blocks. This can emit spurious tool_call events when text or thinking blocks stop after tool_use blocks have started.

      Track the block type for each content block index during content_block_start and use event.index in the content_block_stop handler to confirm the stopped block is a tool_use before emitting a tool_call.

      @@ -0,0 +486,21 @@
      486+ },
      487+ }
      488+ }
      489+ }
      490+ }
      491+ }
      492+ }
      493+ } catch (error: unknown) {
      494+ const err = error as Error & { status?: number; code?: string }
      495+
      496+ yield {
      497+ type: 'error',
      498+ id: genId(),
      499+ model: model,
      500+ timestamp,
      501+ error: {
      502+ message: err.message || 'Unknown error occurred',
      503+ code: err.code || String(err.status),
      504+ },
      505+ }
      506+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      Duplicate error code issue.

      Same issue as in chatStream: if both err.code and err.status are undefined, the code will be "undefined".

             yield {
               type: 'error',
               id: genId(),
               model: model,
               timestamp,
               error: {
                 message: err.message || 'Unknown error occurred',
      -          code: err.code || String(err.status),
      +          code: err.code ?? (err.status != null ? String(err.status) : 'unknown'),
               },
             }
      📝 Committable suggestion

      ‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

      Suggested change
      607+ } catch (error: unknown) {
      608+ const err = error as Error & { status?: number; code?: string }
      609+
      610+ yield {
      611+ type: 'error',
      612+ id: genId(),
      613+ model: model,
      614+ timestamp,
      615+ error: {
      616+ message: err.message || 'Unknown error occurred',
      617+ code: err.code ?? (err.status != null ? String(err.status) : 'unknown'),
      618+ },
      619+ }
      620+ }
      🤖 Prompt for AI Agents
      In packages/typescript/ai-anthropic/src/adapters/text.ts around lines 493 to
      506, the error object sets code to err.code || String(err.status) which yields
      the string "undefined" when both are undefined; change the assignment to use a
      safe fallback that avoids the literal "undefined" (e.g., prefer err.code if
      present, else convert err.status only if !== undefined/null, otherwise set code
      to undefined or an empty string) so the emitted error.code is either a real
      value or truly absent/empty.
      @@ -0,0 +338,21 @@
      338+ }
      339+
      340+ parts.push({
      341+ functionCall: {
      342+ name: toolCall.function.name,
      343+ args: parsedArgs,
      344+ },
      345+ })
      346+ }
      347+ }
      348+
      349+ if (msg.role === 'tool' && msg.toolCallId) {
      350+ parts.push({
      351+ functionResponse: {
      352+ name: msg.toolCallId,
      353+ response: {
      354+ content: msg.content || '',
      355+ },
      356+ },
      357+ })
      358+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🔴 Critical

      🧩 Analysis chain

      🌐 Web query:

      Gemini API functionResponse name field format tool calling

      💡 Result:

      Short answer: the functionResponse "name" is the functionDeclaration.name string (the tool/function identifier) and must match exactly the declared function/tool name (no spaces or special characters); the functionResponse payload is a JSON/Struct under "response". Example patterns from Gemini docs:

      • When sending a function result back to the model (Python SDK): types.Part.from_function_response(name=tool_call.name, response={"result": result}). [1]
      • JavaScript example: { name: tool_call.name, response: { result } } included as a user part: { role: 'user', parts: [{ functionResponse: function_response_part }] }. [1]
      • API/Vertex docs: functionResponse has fields name (string) and response (Struct JSON object) — the name must be the functionDeclaration.name and the response holds the function output. Also note thought_signature handling when the model provided one for the functionCall; you must return that signature in the same part when echoing results. [3][2]

      References: [1] Google Gemini function-calling docs; [2] Gemini thought-signatures page; [3] Vertex AI GenerateContent functionCall/functionResponse reference.


      🏁 Script executed:

      # Get context around the code in question to understand the data structures
      cd packages/typescript/ai-gemini/src/adapters && sed -n '340,370p' text.ts

      Repository: TanStack/ai

      Length of output: 777


      🏁 Script executed:

      # Search for tool-related definitions and patterns in the codebase
      rg "toolCallId|functionName|toolCall" packages/typescript/ai-gemini/src --type ts -A 2 -B 2

      Repository: TanStack/ai

      Length of output: 16228


      Use the actual function name, not toolCallId, for functionResponse.name.

      According to the Gemini API specification, the functionResponse.name field must be the functionDeclaration.name (the tool/function identifier), not a call ID. The current code uses msg.toolCallId, which violates the API contract and may cause tool result correlation failures. Ensure the tool message includes the actual function name so it can be passed to functionResponse.name.

      🤖 Prompt for AI Agents
      In packages/typescript/ai-gemini/src/adapters/text.ts around lines 349 to 358,
      the code sets functionResponse.name to msg.toolCallId but must use the actual
      function/tool identifier; change the assignment to use the tool/function name
      (e.g., msg.toolName or msg.tool?.name or msg.functionDeclaration?.name) rather
      than the call ID, preserving toolCallId separately if needed; ensure the message
      construction passes that function name into functionResponse.name so it matches
      the Gemini API contract.
      @@ -0,0 +81,21 @@
      81+ ) {
      82+ if (typeof hostOrClient === 'string' || hostOrClient === undefined) {
      83+ this.client = createOllamaClient({ host: hostOrClient })
      84+ } else {
      85+ this.client = hostOrClient
      86+ }
      87+ this.defaultModel = options.model ?? 'llama3'
      88+ }
      89+
      90+ async summarize(options: SummarizationOptions): Promise<SummarizationResult> {
      91+ const model = options.model || this.defaultModel
      92+
      93+ const prompt = this.buildSummarizationPrompt(options)
      94+
      95+ const response = await this.client.generate({
      96+ model,
      97+ prompt,
      98+ options: {
      99+ temperature: 0.3,
      100+ num_predict: options.maxLength ?? 500,
      101+ },
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      Semantic mismatch: maxLength used as both token limit and word limit.

      num_predict limits output tokens (line 100), but the prompt instructs the model to keep the summary under maxLength words (line 136). Tokens and words are not equivalent—tokens are typically ~0.75 words on average. This inconsistency may cause unexpected truncation or overlong summaries.

      Consider clarifying the semantics or using separate parameters:

             options: {
               temperature: 0.3,
      -        num_predict: options.maxLength ?? 500,
      +        // Rough estimate: ~1.5 tokens per word for safety margin
      +        num_predict: options.maxLength ? Math.ceil(options.maxLength * 1.5) : 500,
             },
      📝 Committable suggestion

      ‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

      Suggested change
      101+ options: {
      102+ temperature: 0.3,
      103+ // Rough estimate: ~1.5 tokens per word for safety margin
      104+ num_predict: options.maxLength ? Math.ceil(options.maxLength * 1.5) : 500,
      105+ },
      🤖 Prompt for AI Agents
      In packages/typescript/ai-ollama/src/adapters/summarize.ts around lines 98 to
      101, the code uses options.maxLength as num_predict (token limit) while the
      prompt asks for a word limit, causing a mismatch; either convert the word limit
      to an estimated token limit before assigning num_predict (e.g., tokens =
      Math.ceil((options.maxLength ?? 500) / 0.75)) or add a separate
      options.maxTokens param and use that for num_predict, and update the prompt to
      reference the same unit (tokens or words) so both the prompt and num_predict use
      the same semantic unit.
      @@ -0,0 +147,21 @@
      147+ function: { index: number }
      148+ }
      149+ return {
      150+ type: 'tool_call',
      151+ id: responseId,
      152+ model: chunk.model,
      153+ timestamp,
      154+ toolCall: {
      155+ type: 'function',
      156+ id: actualToolCall.id,
      157+ function: {
      158+ name: actualToolCall.function.name || '',
      159+ arguments:
      160+ typeof actualToolCall.function.arguments === 'string'
      161+ ? actualToolCall.function.arguments
      162+ : JSON.stringify(actualToolCall.function.arguments),
      163+ },
      164+ },
      165+ index: actualToolCall.function.index,
      166+ }
      167+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🔴 Critical

      🧩 Analysis chain

      🏁 Script executed:

      #!/bin/bash
      # First, find the ToolCall type definition in the codebase
      rg -n "interface ToolCall|type ToolCall" --type=ts -B2 -A10

      Repository: TanStack/ai

      Length of output: 17652


      🏁 Script executed:

      #!/bin/bash
      # Check imports at the top of the file
      head -30 packages/typescript/ai-ollama/src/adapters/text.ts

      Repository: TanStack/ai

      Length of output: 703


      🏁 Script executed:

      #!/bin/bash
      # Search for where ToolCall comes from
      rg -n "from.*ToolCall|import.*ToolCall" --type=ts packages/typescript/ai-ollama/

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      #!/bin/bash
      # Check the actual context around lines 144-167
      sed -n '140,170p' packages/typescript/ai-ollama/src/adapters/text.ts

      Repository: TanStack/ai

      Length of output: 1066


      🏁 Script executed:

      #!/bin/bash
      # Search for Ollama package type definitions in node_modules or check online docs
      rg "interface ToolCall|type ToolCall" packages/typescript/ai-ollama/ -B3 -A8

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      #!/bin/bash
      # Check if there are any Ollama type definitions in the repo
      find . -name "*.d.ts" -o -name "ollama*" | grep -E "(ollama|types)" | head -20

      Repository: TanStack/ai

      Length of output: 134


      🏁 Script executed:

      #!/bin/bash
      # Search for how Ollama ToolCall is used elsewhere in the codebase
      rg "toolCall\." packages/typescript/ai-ollama/src/ -B2 -A2

      Repository: TanStack/ai

      Length of output: 3675


      🏁 Script executed:

      #!/bin/bash
      # Let's verify the Ollama package version and check if ToolCall has id
      grep -A 5 "ollama" packages/typescript/ai-ollama/package.json

      Repository: TanStack/ai

      Length of output: 591


      🏁 Script executed:

      #!/bin/bash
      # Check the complete type assertion and how it's being used
      sed -n '144,170p' packages/typescript/ai-ollama/src/adapters/text.ts

      Repository: TanStack/ai

      Length of output: 955


      🏁 Script executed:

      #!/bin/bash
      # Search for Ollama ToolCall type definition online or in types
      # Let's check if there are any type stubs or documentation
      rg -i "interface ToolCall|export type ToolCall" --type=ts --type=d.ts -B5 -A10 | head -100

      Repository: TanStack/ai

      Length of output: 83


      🏁 Script executed:

      #!/bin/bash
      # Check if Ollama's ToolCall is actually optional for id by looking at how it's used
      # If the code needs to assert id: string, it might mean Ollama's type doesn't guarantee it
      rg "ToolCall" packages/typescript/ai-ollama/src/adapters/text.ts -B3 -A3

      Repository: TanStack/ai

      Length of output: 2128


      🏁 Script executed:

      #!/bin/bash
      # Let's check the Ollama npm package types by examining package.json and looking for type hints
      cd packages/typescript/ai-ollama && npm ls ollama 2>/dev/null | head -20

      Repository: TanStack/ai

      Length of output: 142


      🌐 Web query:

      Ollama ToolCall type definition JavaScript npm package v0.6.3

      💡 Result:

      ToolCall (from ollama v0.6.3 — src/interfaces.ts):

      export interface ToolCall { function: { name: string; arguments: { [key: string]: any; }; }; }

      Source: ollama-js v0.6.3 interfaces.ts. [1]

      References [1] ollama/ollama-js@v0.6.3 — src/interfaces.ts (raw).


      Ollama's ToolCall type lacks an id property—the type assertion masks a real issue.

      Ollama v0.6.3's ToolCall interface only provides function.name and function.arguments, with no id or type properties. The type assertion on lines 145–148 bypasses TypeScript's type safety but doesn't guarantee id exists at runtime. Line 156 uses actualToolCall.id without a fallback, risking undefined access.

      Add a guard or use responseId as the tool call identifier:

      const handleToolCall = (toolCall: ToolCall): StreamChunk => {
        return {
          type: 'tool_call',
          id: responseId,
          model: chunk.model,
          timestamp,
          toolCall: {
            type: 'function',
      -     id: actualToolCall.id,
      +     id: responseId, // Use response ID instead of undefined toolCall.id

      Committable suggestion skipped: line range outside the PR's diff.

      🤖 Prompt for AI Agents
      In packages/typescript/ai-ollama/src/adapters/text.ts around lines 144 to 167,
      the code unsafely asserts a ToolCall has an id and type and then uses
      actualToolCall.id which may be undefined at runtime; replace the assertion with
      runtime guards and fallbacks: check whether toolCall has an id and a
      function.index before using them, use responseId as the toolCall id fallback
      when actualToolCall.id is missing, ensure the returned toolCall.type is set to
      'function' explicitly, and serialize function.arguments safely (with
      JSON.stringify fallback) so no undefined properties are accessed.
      packages/typescript/ai-openai/src/adapters/text.ts line 129OutdatedResolvedby AlemTuzlak
    5. Actionable comments posted: 2

      ♻️ Duplicate comments (1)
      examples/ts-react-chat/src/routes/api.tanchat.ts (1)

      2-8: Remove top-level test = ai(openaiImage(...)) and associated imports

      This const test = ai({ adapter: openaiImage(), ... }) is:

      • Unused in the route logic, and
      • For image adapters, side‑effectful: the ai() implementation immediately calls adapter.generateImages, so this will generate images at module load time for every server start / hot reload.

      A very similar unused test block was flagged previously; this is effectively the same issue with a valid model instead of 'gpt-5'.

      You can safely drop it and the now‑unused imports (ai, openaiImage, openaiSummarize, openaiText, z), e.g.:

      -import { ai, chat, maxIterations, toStreamResponse } from '@tanstack/ai'
      -import {
      -  openai,
      -  openaiImage,
      -  openaiSummarize,
      -  openaiText,
      -} from '@tanstack/ai-openai'
      +import { chat, maxIterations, toStreamResponse } from '@tanstack/ai'
      +import { openai } from '@tanstack/ai-openai'
      @@
      -import z from 'zod'
      @@
      -const test = ai({
      -  adapter: openaiImage(),
      -  model: 'gpt-image-1',
      -  prompt:
      -    'A cute baby sea otter wearing a beret and glasses, sitting at a small cafe table, sipping a cappuccino, with a croissant on a plate. The background shows a picturesque Parisian street with the Eiffel Tower in the distance. The scene is bathed in the warm, golden light of a late afternoon.',
      -  numberOfImages: 2,
      -  size: 'auto',
      -  providerOptions: {
      -    quality: 'high',
      -  },
      -})
      -

      That keeps the example route focused on the chat flow without unexpected image API calls.

      Also applies to: 12-12, 21-31

      🧹 Nitpick comments (9)
      packages/typescript/ai-gemini/src/model-meta.ts (1)

      223-253: Gemini image model metadata and exports look consistent

      The new image model entries (GEMINI_2_5_FLASH_IMAGE, GEMINI_2_FLASH_IMAGE, IMAGEN_* variants) and the GEMINI_IMAGE_MODELS / GeminiImageModels exports line up correctly and give the image adapter a clean, typed model surface. Only nit: you might consider moving GeminiImageModels below GEMINI_IMAGE_MODELS to avoid the forward reference and mirror the pattern used for GeminiModels, but that’s purely cosmetic.

      Also applies to: 421-450, 517-607, 782-793

      packages/typescript/ai/src/core/generate.ts (1)

      24-281: Unified ai() routing and typing look correct

      The GenerateAdapter/AnyGenerateAdapter unions, GenerateOptionsFor/GenerateReturnType, and the ai() overload cleanly discriminate on adapter.kind and produce the expected API shapes:

      • chat → stream or structured Promise based on outputSchema
      • embedding → Promise<EmbeddingResult>
      • summarize → streaming vs non‑streaming via stream
      • image → Promise<ImageGenerationResult>

      The model/providerOptions extractor helpers (ChatProviderOptionsForModel, ImageProviderOptionsForModel, etc.) also give nice per‑model typing without leaking index‑signature maps. Aside from the summarize providerOptions wiring noted separately, this core looks well‑structured.

      Also applies to: 365-400, 472-598, 664-677, 685-704

      packages/typescript/ai-gemini/tests/image-adapter.test.ts (1)

      115-160: Relax generateImages call expectation to tolerate extra config fields

      The generateImages test asserts:

      expect(mockGenerateImages).toHaveBeenCalledWith({
        model: 'imagen-3.0-generate-002',
        prompt: 'A cat wearing a hat',
        config: {
          numberOfImages: 1,
          aspectRatio: '1:1',
        },
      })

      But the adapter’s buildConfig currently also includes imageSize (and may gain more fields over time). With strict toHaveBeenCalledWith, any additional keys on config will cause this test to fail even though the call is still correct.

      Consider making this more robust, e.g.:

      expect(mockGenerateImages).toHaveBeenCalledWith({
        model: 'imagen-3.0-generate-002',
        prompt: 'A cat wearing a hat',
        config: expect.objectContaining({
          numberOfImages: 1,
          aspectRatio: '1:1',
        }),
      })

      This keeps the important contract assertions while allowing harmless additions to the request config.

      packages/typescript/ai-openai/tests/image-adapter.test.ts (1)

      135-187: Make generateImages request expectations less brittle

      The tests currently assert the full images.generate call shape:

      expect(mockGenerate).toHaveBeenCalledWith({
        model: 'gpt-image-1',
        prompt: 'A cat wearing a hat',
        n: 1,
        size: '1024x1024',
        stream: false,
      })

      This will start failing as soon as buildRequest adds legitimate fields (e.g., response_format, quality/style options).

      You can keep the important guarantees while future‑proofing the test by loosening it slightly:

      expect(mockGenerate).toHaveBeenCalledWith(
        expect.objectContaining({
          model: 'gpt-image-1',
          prompt: 'A cat wearing a hat',
          n: 1,
          size: '1024x1024',
          stream: false,
        }),
      )

      Same idea applies to the second test that only checks ID generation; the call shape doesn’t need to be pinned exactly there either.

      Also applies to: 189-218

      packages/typescript/ai-gemini/src/adapters/image.ts (1)

      90-121: Minor cleanup in config/result shaping

      Two small nits you might consider:

      • In buildConfig, imageSize: size will serialize away when size is undefined, so it’s harmless but could be omitted entirely when not set for slightly cleaner requests.
      • In transformResponse, you don’t need to explicitly set usage: undefined; omitting the property keeps the result shape cleaner and avoids consumers checking for an explicitly undefined field.

      Example tweak:

        private buildConfig(
          options: ImageGenerationOptions<GeminiImageProviderOptions>,
        ): GenerateImagesConfig {
          const { size, numberOfImages, providerOptions } = options
      
      -   return {
      -     numberOfImages: numberOfImages ?? 1,
      -     imageSize: size,
      -     aspectRatio: size ? sizeToAspectRatio(size) : undefined,
      -     ...providerOptions,
      -   }
      +   return {
      +     numberOfImages: numberOfImages ?? 1,
      +     ...(size && { imageSize: size, aspectRatio: sizeToAspectRatio(size) }),
      +     ...providerOptions,
      +   }
        }
      
        private transformResponse(
          model: string,
          response: GenerateImagesResponse,
        ): ImageGenerationResult {
          const images: Array<GeneratedImage> = (response.generatedImages ?? []).map(
            (item) => ({
              b64Json: item.image?.imageBytes,
              revisedPrompt: item.enhancedPrompt,
            }),
          )
      
          return {
            id: generateId(this.name),
            model,
            images,
      -     usage: undefined,
          }
        }
      packages/typescript/ai-openai/src/adapters/image.ts (1)

      59-92: Double‑check size: 'auto' semantics across models

      Right now generateImages relies on validateImageSize, which short‑circuits for size === 'auto', so 'auto' is effectively allowed for any model. If DALL·E 2/3 do not support 'auto' for size, these calls will only fail at the OpenAI API level instead of being caught by your validator.

      A tighter approach (if the API requires it) would be to only treat 'auto' as special for the GPT‑Image models and run validation (and/or throw) for 'auto' on DALL·E:

      export function validateImageSize(
        model: string,
        size: string | undefined,
      ): void {
      -  if (!size || size === 'auto') return
      +  if (!size) return
      +
      +  const isGptImageModel =
      +    model === 'gpt-image-1' || model === 'gpt-image-1-mini'
      +  if (size === 'auto' && isGptImageModel) return

      Please verify this against the current OpenAI image docs before changing behavior.

      packages/typescript/ai-openai/src/image/image-provider-options.ts (2)

      213-237: Clarify validateImageSize behavior for 'auto' and per‑model sizes

      The validator short‑circuits for size === 'auto', which means 'auto' is effectively accepted for all models even though only the GPT‑Image models include 'auto' in their documented size sets. If DALL·E 2/3 don’t support 'auto', this defeats the purpose of early validation.

      Consider:

      • Restricting the 'auto' fast‑path to GPT‑Image models only, and
      • Potentially using OpenAIImageModelSizeByName (or a typed helper) instead of a raw Record<string, Array<string>> to keep the table aligned with the type aliases.

      This keeps runtime validation in sync with the declared capabilities.


      264-297: Optional: enforce documented background/output_format constraint

      The JSDoc for background says that when 'transparent', output_format must be 'png' or 'webp', but validateBackground currently only guards that background is used with GPT‑Image models, not that the output format is compatible.

      If you want runtime parity with the docs (especially for JS consumers or any usage), consider extending the validation input to include output_format and adding a simple check:

      -interface ImageValidationOptions {
      -  prompt: string
      -  model: string
      -  background?: 'transparent' | 'opaque' | 'auto' | null
      -}
      +interface ImageValidationOptions {
      +  prompt: string
      +  model: string
      +  background?: 'transparent' | 'opaque' | 'auto' | null
      +  output_format?: GptImageOutputFormat
      +}
      
      export const validateBackground = (options: ImageValidationOptions) => {
        if (options.background) {
          const supportedModels = ['gpt-image-1', 'gpt-image-1-mini']
          if (!supportedModels.includes(options.model)) {
            throw new Error(
              `The model ${options.model} does not support background option.`,
            )
          }
      +
      +    if (
      +      options.background === 'transparent' &&
      +      options.output_format &&
      +      options.output_format !== 'png' &&
      +      options.output_format !== 'webp'
      +    ) {
      +      throw new Error(
      +        `Transparent background requires output_format 'png' or 'webp', got '${options.output_format}'.`,
      +      )
      +    }
        }
      }
      packages/typescript/ai/src/types.ts (1)

      1044-1093: ChatOptionsForChatAdapter mirrors ChatStreamOptionsForModel; minor Omit nit

      The ChatOptionsForChatAdapter helper matches the structure of ChatStreamOptionsForModel, constraining providerOptions by model and messages by model input modalities and metadata, which is exactly what you want for the new ChatAdapter abstraction.

      One tiny nit: the Omit<ChatOptions, 'model' | 'providerOptions' | 'responseFormat' | 'messages'> still refers to 'responseFormat', while the current options use output?: ResponseFormat<...>. This is harmless (the extra key is ignored in Omit), but you could replace 'responseFormat' with 'output' for accuracy:

      -    ? Omit<
      -        ChatOptions,
      -        'model' | 'providerOptions' | 'responseFormat' | 'messages'
      -      > & {
      +    ? Omit<ChatOptions, 'model' | 'providerOptions' | 'output' | 'messages'> & {
      📜 Review details

      Configuration used: CodeRabbit UI

      Review profile: CHILL

      Plan: Pro

      📥 Commits

      Reviewing files that changed from the base of the PR and between 86fa305bf3a71f1f1711f656d5133bfc4e5d4df3 and 5c9703c4bd4bee0654fd77fd60bff609e40485ca.

      📒 Files selected for processing (18)
      • README.md (1 hunks)
      • docs/guides/image-generation.md (1 hunks)
      • examples/ts-react-chat/src/routes/api.tanchat.ts (2 hunks)
      • packages/typescript/ai-gemini/src/adapters/image.ts (1 hunks)
      • packages/typescript/ai-gemini/src/image/image-provider-options.ts (1 hunks)
      • packages/typescript/ai-gemini/src/index.ts (1 hunks)
      • packages/typescript/ai-gemini/src/model-meta.ts (10 hunks)
      • packages/typescript/ai-gemini/tests/image-adapter.test.ts (1 hunks)
      • packages/typescript/ai-openai/src/adapters/image.ts (1 hunks)
      • packages/typescript/ai-openai/src/image/image-provider-options.ts (2 hunks)
      • packages/typescript/ai-openai/src/index.ts (1 hunks)
      • packages/typescript/ai-openai/src/model-meta.ts (5 hunks)
      • packages/typescript/ai-openai/tests/image-adapter.test.ts (1 hunks)
      • packages/typescript/ai/src/adapters/base-image-adapter.ts (1 hunks)
      • packages/typescript/ai/src/adapters/index.ts (1 hunks)
      • packages/typescript/ai/src/core/generate.ts (1 hunks)
      • packages/typescript/ai/src/index.ts (1 hunks)
      • packages/typescript/ai/src/types.ts (3 hunks)
      ✅ Files skipped from review due to trivial changes (1)
      • docs/guides/image-generation.md
      🚧 Files skipped from review as they are similar to previous changes (2)
      • README.md
      • packages/typescript/ai/src/adapters/index.ts
      🧰 Additional context used
      🧬 Code graph analysis (5)
      packages/typescript/ai-openai/tests/image-adapter.test.ts (3)
      packages/typescript/ai-openai/src/adapters/image.ts (2)
      • createOpenaiImage (137-142)
      • OpenAIImageAdapter (42-117)
      packages/typescript/ai-openai/src/index.ts (2)
      • createOpenaiImage (35-35)
      • OpenAIImageAdapter (34-34)
      packages/typescript/ai-openai/src/image/image-provider-options.ts (3)
      • validateImageSize (213-237)
      • validateNumberOfImages (242-262)
      • validatePrompt (275-297)
      packages/typescript/ai-gemini/tests/image-adapter.test.ts (3)
      packages/typescript/ai-gemini/src/adapters/image.ts (2)
      • createGeminiImage (142-147)
      • GeminiImageAdapter (48-122)
      packages/typescript/ai-gemini/src/index.ts (2)
      • createGeminiImage (39-39)
      • GeminiImageAdapter (38-38)
      packages/typescript/ai-gemini/src/image/image-provider-options.ts (4)
      • sizeToAspectRatio (180-185)
      • validateImageSize (191-206)
      • validateNumberOfImages (212-226)
      • validatePrompt (231-239)
      packages/typescript/ai-openai/src/image/image-provider-options.ts (1)
      packages/typescript/ai-openai/src/index.ts (2)
      • OpenAIImageProviderOptions (40-40)
      • OpenAIImageModelProviderOptionsByName (41-41)
      examples/ts-react-chat/src/routes/api.tanchat.ts (4)
      packages/typescript/ai/src/core/generate.ts (1)
      • ai (375-466)
      packages/typescript/ai/src/index.ts (1)
      • ai (5-5)
      packages/typescript/ai-openai/src/adapters/image.ts (1)
      • openaiImage (167-172)
      packages/typescript/ai-openai/src/index.ts (1)
      • openaiImage (36-36)
      packages/typescript/ai-openai/src/adapters/image.ts (5)
      packages/typescript/ai/src/adapters/index.ts (1)
      • BaseImageAdapter (29-29)
      packages/typescript/ai-openai/src/model-meta.ts (1)
      • OPENAI_IMAGE_MODELS (1656-1661)
      packages/typescript/ai-openai/src/image/image-provider-options.ts (6)
      • OpenAIImageProviderOptions (173-177)
      • OpenAIImageModelProviderOptionsByName (183-188)
      • OpenAIImageModelSizeByName (193-198)
      • validatePrompt (275-297)
      • validateImageSize (213-237)
      • validateNumberOfImages (242-262)
      packages/typescript/ai/src/types.ts (3)
      • ImageGenerationOptions (746-759)
      • ImageGenerationResult (776-789)
      • GeneratedImage (764-771)
      packages/typescript/ai/src/adapters/base-image-adapter.ts (1)
      • generateId (88-90)
      ⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
      • GitHub Check: Test
      🔇 Additional comments (15)
      packages/typescript/ai/src/adapters/base-image-adapter.ts (1)

      1-91: BaseImageAdapter contract and generics look solid

      The shared ImageAdapter interface and BaseImageAdapter implementation are well-shaped for the OpenAI/Gemini adapters (models, providerOptions, size maps, config, and kind: 'image'). The type-only declare fields are a good pattern for inference without runtime cost; no issues spotted.

      packages/typescript/ai/src/index.ts (1)

      4-13: Re-exporting ai and generate option types from the root is appropriate

      The new export block cleanly exposes ai, AnyAdapter, GenerateAdapter, GenerateOptions, and the per-kind option aliases from ./core/generate without disturbing existing exports. This is the right place for the new unified entrypoint.

      packages/typescript/ai-openai/src/model-meta.ts (1)

      1250-1290: Image model metadata and OPENAI_IMAGE_MODELS export are aligned

      Defining DALL_E_2 and DALL_E_3 as proper ModelMeta entries and exporting OPENAI_IMAGE_MODELS with GPT_IMAGE_1, GPT_IMAGE_1_MINI, dall-e-3, and dall-e-2 matches the adapter/tests expectations and cleanly separates image models from chat/embedding lists. No issues spotted here.

      Also applies to: 1656-1661

      packages/typescript/ai-gemini/src/adapters/image.ts (2)

      31-60: Adapter generics and type integration look correct

      GeminiImageAdapter correctly threads GEMINI_IMAGE_MODELS, GeminiImageProviderOptions, and the model→options/size maps into BaseImageAdapter, and the declared _modelProviderOptionsByName / _modelSizeByName fields give good type narrowing for consumers.


      124-177: Factory helpers are consistent and ergonomic

      createGeminiImage and geminiImage mirror the other adapters’ factory patterns (explicit key vs env lookup) and correctly compose GeminiImageConfig by omitting apiKey from the external config surface.

      packages/typescript/ai-openai/src/adapters/image.ts (3)

      31-58: Adapter wiring and client construction look solid

      OpenAIImageAdapter cleanly wires OPENAI_IMAGE_MODELS, the provider options types, and the createOpenAIClient-based client. Class shape matches the other tree‑shakeable adapters, which keeps the surface consistent.


      94-116: Response mapping matches the OpenAI images response shape

      Mapping data entries to GeneratedImage (b64, url, revisedPrompt) and normalizing response.usage into the generic {inputTokens, outputTokens, totalTokens} shape looks correct and matches the rest of the SDK’s conventions.


      119-172: Factory functions follow the established OpenAI adapter pattern

      createOpenaiImage and openaiImage mirror the text/embed/summarize factories (explicit key vs env lookup, Omit<...,'apiKey'>), which should make migration straightforward for users.

      packages/typescript/ai-openai/src/index.ts (1)

      5-75: Export surface for tree‑shakeable adapters and legacy adapter looks well‑structured

      The index cleanly groups the new text/embed/summarize/image adapters with their configs and provider options, and clearly marks the monolithic OpenAI export as deprecated. This should make the migration path obvious without breaking existing users.

      packages/typescript/ai-gemini/src/index.ts (1)

      5-91: Gemini index exports align well with the new adapter architecture

      The file cleanly exposes the new text/embed/summarize/image adapters and model aliases, while keeping the monolithic GeminiAdapter under a clearly documented deprecated section. The symmetry with the OpenAI index is helpful for users switching providers.

      packages/typescript/ai/src/types.ts (3)

      738-789: Image generation core types match adapter usage

      ImageGenerationOptions, GeneratedImage, and ImageGenerationResult line up with how the OpenAI and Gemini image adapters use them (model/prompt/numberOfImages/size/providerOptions in, images + optional usage out). Centralizing these here should make it easy to add other providers without duplicating shapes.


      1016-1043: Adapter model‑extraction helpers are consistent with existing patterns

      ExtractModelsFromChatAdapter, ExtractModelsFromEmbeddingAdapter, and ExtractModelsFromSummarizeAdapter (plus ExtractModelsFromAnyAdapter) mirror the existing ExtractModelsFromAdapter style and correctly pull M[number] out of each adapter’s first generic. This will be handy for strongly‑typed higher‑level helpers that accept any of the new adapter types.


      1092-1093: Re‑exporting adapter types from ./adapters is useful for consumers

      The final export type { ChatAdapter, EmbeddingAdapter, SummarizeAdapter, AnyAdapter } makes these new abstractions discoverable from the central @tanstack/ai types module, which is convenient for library users.

      packages/typescript/ai-gemini/src/image/image-provider-options.ts (2)

      29-153: Gemini image provider options and model maps look well‑designed

      GeminiImageProviderOptions mirrors the GenerateImagesConfig surface (aspect ratio, safety, watermarking, compression, etc.), and the model→options/size maps (GeminiImageModelProviderOptionsByName, GeminiImageModelSizeByName) give you nice type‑level narrowing for adapters without over‑committing to specific models in this file.


      191-239: Size, count, and prompt validators are reasonable defaults

      validateImageSize’s use of sizeToAspectRatio with a clear error message, validateNumberOfImages clamping to 1–4, and validatePrompt’s trimmed non‑empty check give good early feedback before hitting the Gemini API. The comments about model‑specific max‑images provide context for the conservative maxImages = 4 choice.

      packages/typescript/ai/src/core/generate.ts lines 185 to 225OutdatedResolvedby AlemTuzlak
    6. Actionable comments posted: 9

      ♻️ Duplicate comments (11)
      packages/typescript/ai-gemini/src/adapters/text.ts (3)

      325-332: Incorrect token count mapping—use candidatesTokenCount instead of thoughtsTokenCount.

      This issue was flagged in a previous review and remains unresolved. thoughtsTokenCount represents internal reasoning tokens (chain-of-thought), not completion tokens. Use candidatesTokenCount for actual generated response tokens.

      Apply this diff:

                usage: chunk.usageMetadata
                  ? {
                      promptTokens: chunk.usageMetadata.promptTokenCount ?? 0,
      -               completionTokens: chunk.usageMetadata.thoughtsTokenCount ?? 0,
      +               completionTokens: chunk.usageMetadata.candidatesTokenCount ?? 0,
                      totalTokens: chunk.usageMetadata.totalTokenCount ?? 0,
                    }
                  : undefined,

      341-365: Incorrect default MIME type for non-image content.

      This issue was flagged in a previous review and remains unresolved. The fallback 'image/jpeg' is applied to audio, video, and document types, which will cause processing failures for non-image content.

      Apply type-appropriate defaults for each media type:

            case 'image':
      +     case 'audio':
      +     case 'video':
      +     case 'document': {
      -     case 'audio':
      -     case 'video':
      -     case 'document': {
      -       const metadata = part.metadata as
      -         | GeminiDocumentMetadata
      -         | GeminiImageMetadata
      -         | GeminiVideoMetadata
      -         | GeminiAudioMetadata
      -         | undefined
      +       let defaultMimeType: string
      +       let metadata:
      +         | GeminiDocumentMetadata
      +         | GeminiImageMetadata
      +         | GeminiVideoMetadata
      +         | GeminiAudioMetadata
      +         | undefined
      +
      +       switch (part.type) {
      +         case 'image':
      +           defaultMimeType = 'image/jpeg'
      +           metadata = part.metadata as GeminiImageMetadata | undefined
      +           break
      +         case 'audio':
      +           defaultMimeType = 'audio/mp3'
      +           metadata = part.metadata as GeminiAudioMetadata | undefined
      +           break
      +         case 'video':
      +           defaultMimeType = 'video/mp4'
      +           metadata = part.metadata as GeminiVideoMetadata | undefined
      +           break
      +         case 'document':
      +           defaultMimeType = 'application/pdf'
      +           metadata = part.metadata as GeminiDocumentMetadata | undefined
      +           break
      +       }
      +
              if (part.source.type === 'data') {
                return {
                  inlineData: {
                    data: part.source.value,
      -             mimeType: metadata?.mimeType ?? 'image/jpeg',
      +             mimeType: metadata?.mimeType ?? defaultMimeType,
                  },
                }
              } else {
                return {
                  fileData: {
                    fileUri: part.source.value,
      -             mimeType: metadata?.mimeType ?? 'image/jpeg',
      +             mimeType: metadata?.mimeType ?? defaultMimeType,
                  },
                }
              }
            }

      417-426: Use function name instead of toolCallId for functionResponse.name.

      This issue was flagged in a previous review and remains unresolved. Per the Gemini API specification, functionResponse.name must be the function/tool identifier (the declaration name), not a call ID.

      The ModelMessage type for tool messages needs to include the function name. Assuming the message structure includes it (e.g., as msg.toolName or similar), apply this diff:

            if (msg.role === 'tool' && msg.toolCallId) {
              parts.push({
                functionResponse: {
      -           name: msg.toolCallId,
      +           name: msg.toolName, // or the appropriate property containing the function name
                  response: {
                    content: msg.content || '',
                  },
                },
              })
            }

      If the tool message doesn't currently include the function name, the message type definition must be updated to include it.

      packages/typescript/ai/src/core/generate.ts (1)

      542-560: Summarize providerOptions not forwarded - previously flagged.

      This issue was already identified in a previous review. The providerOptions from GenerateSummarizeOptions is not being passed through to SummarizationOptions in both generateSummary and generateSummaryStream.

      packages/typescript/ai-anthropic/src/adapters/text.ts (4)

      109-121: Error code may produce string "undefined" - previously flagged.

      This issue was already identified in a previous review. When both err.code and err.status are undefined, String(err.status) produces "undefined".


      230-239: Spread of validProviderOptions can override systemPrompts - previously flagged.

      This issue was already identified in a previous review. The 'system' key is in validKeys, so spreading validProviderOptions after setting system from systemPrompts can override it.


      473-491: content_block_stop handler doesn't use event.index - previously flagged.

      This issue was already identified in a previous review. The handler checks toolCallsMap.get(currentToolIndex) without verifying the stopped block is actually a tool_use block using event.index.


      554-567: Duplicate error code issue - previously flagged.

      This issue was already identified in a previous review. Same pattern as line 118 where err.code || String(err.status) can produce "undefined".

      packages/typescript/ai-ollama/src/adapters/text.ts (1)

      190-213: Ollama ToolCall lacks id property - previously flagged.

      This issue was already identified in a previous review. Ollama v0.6.3's ToolCall interface doesn't include id or function.index properties. The type assertion masks this, but actualToolCall.id and actualToolCall.function.index will be undefined at runtime.

      packages/typescript/ai-openai/src/adapters/text.ts (2)

      210-210: Unused eventTypeCounts variable.

      The eventTypeCounts map is declared but never populated. It's referenced in the error logging at line 467 but will always be empty. Either implement the counting logic or remove this variable and its reference.


      647-652: Base64 images still need data URI format.

      The comment at line 647 states "For base64 data, construct a data URI" but the code returns part.source.value directly without the data URI prefix. OpenAI's API expects image_url to be a data URI like data:image/jpeg;base64,... for base64 data.

             // For base64 data, construct a data URI
      +      const mediaType = part.metadata?.mediaType || 'image/jpeg'
             return {
               type: 'input_image',
      -        image_url: part.source.value,
      +        image_url: `data:${mediaType};base64,${part.source.value}`,
               detail: imageMetadata?.detail || 'auto',
             }
      🧹 Nitpick comments (5)
      packages/typescript/ai/src/core/generate.ts (2)

      283-300: Missing newlines before type/comment blocks.

      Lines 283 and 300 are missing newlines before the comment/type definition, which reduces readability. The never type ends directly before the next type definition without separation.

      -        : never /** Infer the return type based on adapter kind, schema, and stream */
      -type GenerateReturnType<
      +        : never
      +
      +/** Infer the return type based on adapter kind, schema, and stream */
      +type GenerateReturnType<

      And similarly at line 300:

      -        : never /**
      +        : never
      +
      +/**

      481-482: Avoid as any cast - loses type safety.

      The chat(options as any) cast bypasses TypeScript's type checking entirely. Consider defining a more specific type or creating a proper mapping function.

       async function* generateChat(
         options: GenerateChatOptions<
           ChatAdapter<ReadonlyArray<string>, object, any, any, any>,
           string,
           z.ZodType | undefined
         >,
       ): AsyncIterable<StreamChunk> {
      -  yield* chat(options as any)
      +  yield* options.adapter.chatStream({
      +    model: options.model,
      +    messages: options.messages,
      +    systemPrompts: options.systemPrompts,
      +    tools: options.tools,
      +    options: options.options,
      +    providerOptions: options.providerOptions,
      +    abortController: options.abortController,
      +    agentLoopStrategy: options.agentLoopStrategy,
      +    conversationId: options.conversationId,
      +  })
       }
      packages/typescript/ai/src/adapters/base-chat-adapter.ts (1)

      159-161: Consider using crypto.randomUUID() for more robust ID generation.

      Math.random() has limited entropy and can produce collisions in high-volume scenarios. For a base adapter that may be used across many requests, crypto.randomUUID() provides better uniqueness guarantees.

         protected generateId(): string {
      -    return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`
      +    return `${this.name}-${Date.now()}-${crypto.randomUUID().slice(0, 8)}`
         }

      Note: If browser compatibility without crypto API is needed, the current implementation is acceptable.

      packages/typescript/ai-ollama/src/adapters/text.ts (1)

      300-307: Redundant conditional branches for image source handling.

      Both branches of the conditional do the same thing - push part.source.value to images. This can be simplified.

                 } else if (part.type === 'image') {
      -            if (part.source.type === 'data') {
      -              images.push(part.source.value)
      -            } else {
      -              images.push(part.source.value)
      -            }
      +            images.push(part.source.value)
                 }
      packages/typescript/ai-openai/src/adapters/text.ts (1)

      104-111: Consider reducing verbose error logging in production.

      The error logging outputs full stack traces and error objects to console.error. While useful for debugging, this could be noisy in production environments. Consider using a logging abstraction with configurable levels or reducing the verbosity.

           } catch (error: unknown) {
             const err = error as Error
      -      console.error('>>> chatStream: Fatal error during response creation <<<')
      -      console.error('>>> Error message:', err.message)
      -      console.error('>>> Error stack:', err.stack)
      -      console.error('>>> Full error:', err)
      +      // Log error details for debugging
      +      console.error('[OpenAI Adapter] chatStream error:', err.message)
             throw error
           }
      📜 Review details

      Configuration used: CodeRabbit UI

      Review profile: CHILL

      Plan: Pro

      📥 Commits

      Reviewing files that changed from the base of the PR and between 5c9703c4bd4bee0654fd77fd60bff609e40485ca and 14c429704b36287aeb26d592b821948160ebdf0b.

      ⛔ Files ignored due to path filters (1)
      • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
      📒 Files selected for processing (11)
      • packages/typescript/ai-anthropic/src/adapters/text.ts (1 hunks)
      • packages/typescript/ai-gemini/src/adapters/image.ts (1 hunks)
      • packages/typescript/ai-gemini/src/adapters/text.ts (1 hunks)
      • packages/typescript/ai-ollama/src/adapters/text.ts (1 hunks)
      • packages/typescript/ai-openai/src/adapters/text.ts (1 hunks)
      • packages/typescript/ai/src/adapters/base-chat-adapter.ts (1 hunks)
      • packages/typescript/ai/src/adapters/index.ts (1 hunks)
      • packages/typescript/ai/src/core/generate.ts (1 hunks)
      • packages/typescript/ai/src/types.ts (4 hunks)
      • packages/typescript/ai/tests/generate-types.test-d.ts (1 hunks)
      • packages/typescript/ai/tests/generate.test.ts (1 hunks)
      🚧 Files skipped from review as they are similar to previous changes (1)
      • packages/typescript/ai/tests/generate.test.ts
      🧰 Additional context used
      🧬 Code graph analysis (2)
      packages/typescript/ai-gemini/src/adapters/text.ts (2)
      packages/typescript/ai-gemini/src/utils/client.ts (4)
      • GeminiClientConfig (3-5)
      • createGeminiClient (10-14)
      • generateId (41-43)
      • getGeminiApiKeyFromEnv (20-36)
      packages/typescript/ai-gemini/src/model-meta.ts (3)
      • GEMINI_MODELS (771-780)
      • GeminiChatModelProviderOptionsByName (815-864)
      • GeminiModelInputModalitiesByName (879-891)
      packages/typescript/ai/src/core/generate.ts (6)
      packages/typescript/ai/src/index.ts (5)
      • GenerateAdapter (7-7)
      • ai (5-5)
      • chatOptions (39-39)
      • chat (1-1)
      • convertZodToJsonSchema (26-26)
      packages/typescript/ai/src/adapters/base-chat-adapter.ts (1)
      • ChatAdapter (50-102)
      packages/typescript/ai/src/types.ts (11)
      • ChatAdapter (1100-1100)
      • EmbeddingAdapter (1100-1100)
      • SummarizeAdapter (1100-1100)
      • ModelMessage (226-237)
      • ChatOptions (554-595)
      • StreamChunk (685-693)
      • EmbeddingResult (735-743)
      • SummarizationResult (718-727)
      • ImageGenerationResult (783-796)
      • EmbeddingOptions (729-733)
      • SummarizationOptions (710-716)
      packages/typescript/ai/src/adapters/base-embedding-adapter.ts (1)
      • EmbeddingAdapter (22-41)
      packages/typescript/ai/src/adapters/base-summarize-adapter.ts (1)
      • SummarizeAdapter (22-41)
      packages/typescript/ai/src/adapters/base-image-adapter.ts (1)
      • ImageAdapter (25-52)
      🔇 Additional comments (21)
      packages/typescript/ai-gemini/src/adapters/image.ts (5)

      1-29: LGTM!

      Imports are well-organized with proper separation of type imports using import type. Dependencies are correctly structured.


      31-66: Clean adapter structure.

      The class definition follows the established adapter pattern correctly. The empty GeminiImageConfig interface extending GeminiClientConfig provides a good extension point for future Gemini-specific configuration. The use of declare for type-narrowing properties is an appropriate TypeScript pattern.


      68-88: Well-structured method with proper validation flow.

      The validate-build-call-transform pattern is clean and maintainable. Input validation before the API call correctly prevents unnecessary network requests for invalid inputs.


      141-176: LGTM!

      The factory functions provide a clean API with both explicit key and environment-based instantiation patterns. The JSDoc comments with examples are helpful for consumers.


      103-120: No changes needed. The GeneratedImage interface already defines b64Json as an optional property (b64Json?: string), which correctly allows the potentially undefined value from item.image?.imageBytes. The type is already type-safe.

      packages/typescript/ai-gemini/src/adapters/text.ts (1)

      435-454: Verify config spread order allows intentional overrides.

      The providerOpts.generationConfig is spread after individual temperature, topP, and maxOutputTokens settings, allowing provider options to override common options. This may be intentional, but ensure this precedence aligns with the expected behavior.

      If common options should take precedence, apply this diff:

            config: {
      -       ...providerOpts,
      -       temperature: options.options?.temperature,
      -       topP: options.options?.topP,
      -       maxOutputTokens: options.options?.maxTokens,
      -       systemInstruction: options.systemPrompts?.join('\n'),
              ...((providerOpts as Record<string, unknown>).generationConfig as
                | Record<string, unknown>
                | undefined),
      +       ...providerOpts,
      +       temperature: options.options?.temperature,
      +       topP: options.options?.topP,
      +       maxOutputTokens: options.options?.maxTokens,
      +       systemInstruction: options.systemPrompts?.join('\n'),
              tools: convertToolsToProviderFormat(options.tools),
            },
      packages/typescript/ai/src/adapters/base-chat-adapter.ts (1)

      1-102: Well-structured adapter interface with comprehensive generics.

      The ChatAdapter interface provides excellent type-safety with its five generic parameters covering models, provider options, model-specific options, input modalities, and message metadata. The use of declare for type-only properties is a clean pattern for inference without runtime overhead.

      packages/typescript/ai/src/adapters/index.ts (1)

      1-44: Clean barrel export structure.

      The index file provides a well-organized export surface with clear section comments. The AdapterKind and AnyAdapter union types offer convenient aggregations for consumers.

      packages/typescript/ai-ollama/src/adapters/text.ts (1)

      105-127: Solid adapter initialization with flexible client injection.

      The constructor accepts either a host string or a pre-configured Ollama client, which is good for testing and flexibility. The default model fallback to 'llama3' is reasonable.

      packages/typescript/ai/tests/generate-types.test-d.ts (4)

      48-117: Well-structured mock adapters for type testing.

      The mock adapter implementations correctly extend the base adapters with strict generics and provide minimal implementations sufficient for type-level testing. The empty async generator for chatStream is acceptable since vitest's type tests (test-d.ts files) only perform type checking without runtime execution.


      119-334: Comprehensive type inference test coverage.

      The tests thoroughly validate type inference across all adapter categories, including model validation, strict provider options enforcement, and cross-adapter option restrictions. The use of @ts-expect-error annotations correctly verifies that TypeScript rejects invalid configurations.


      336-340: Acceptable workaround for type-test module imports.

      The require('zod') pattern with the typeof import('zod') cast is a valid workaround for type-level tests to avoid module hoisting issues while maintaining proper type inference. The ESLint disable comment appropriately acknowledges this deviation.


      483-541: Complete streaming behavior type coverage.

      The tests correctly validate that the return type changes based on the stream option for summarize adapters, and that the stream option is properly rejected for chat and embedding adapters.

      packages/typescript/ai-openai/src/adapters/text.ts (5)

      53-74: Well-structured adapter class definition.

      The adapter properly extends BaseChatAdapter with comprehensive generic parameters for model constraints, provider options, and metadata typing. The constructor correctly delegates client creation.


      118-168: Solid structured output implementation.

      The method correctly uses OpenAI's native JSON Schema response format with strict: true for reliable structured output. Error handling appropriately truncates long content in error messages for readability.


      256-288: Good handling of response lifecycle events.

      The code properly handles response created/incomplete/failed events, resets streaming state appropriately, and yields error chunks when the response contains errors or incomplete details.


      461-481: Error chunk properly yielded after stream failure.

      The catch block correctly yields an error chunk to notify consumers of stream failures. Note: The eventTypeCounts reference at line 467 is part of the unused variable issue noted earlier.


      710-757: Well-documented factory functions.

      The factory functions provide clear APIs for both explicit API key usage and environment variable detection. The JSDoc documentation with examples is helpful for developers.

      packages/typescript/ai/src/types.ts (3)

      1030-1097: LGTM! Tree-shakeable adapter type utilities added.

      The new type utilities (ExtractModelsFromChatAdapter, ExtractModelsFromEmbeddingAdapter, ExtractModelsFromSummarizeAdapter, ExtractModelsFromAnyAdapter, and ChatOptionsForChatAdapter) provide proper type inference for the new tree-shakeable adapter architecture.

      Note: There's intentional duplication with existing types like ChatStreamOptionsForModel (lines 947-997), which makes sense for migration purposes. Consider adding a comment explaining this is part of the adapter architecture evolution.


      1100-1100: LGTM! Adapter types re-exported for public API.

      Re-exporting the adapter types from the ./adapters module makes them available as part of the public API surface, which aligns with the tree-shakeable architecture goals mentioned in the PR objectives.


      4-9: All four adapter types (AnyAdapter, ChatAdapter, EmbeddingAdapter, SummarizeAdapter) are properly exported from the ./adapters module. The import statement is correct and valid.

      @@ -0,0 +386,21 @@
      386+ }
      387+ } else if (msg.content) {
      388+ parts.push({ text: msg.content })
      389+ }
      390+
      391+ if (msg.role === 'assistant' && msg.toolCalls?.length) {
      392+ for (const toolCall of msg.toolCalls) {
      393+ let parsedArgs: Record<string, unknown> = {}
      394+ try {
      395+ parsedArgs = toolCall.function.arguments
      396+ ? (JSON.parse(toolCall.function.arguments) as Record<
      397+ string,
      398+ unknown
      399+ >)
      400+ : {}
      401+ } catch {
      402+ parsedArgs = toolCall.function.arguments as unknown as Record<
      403+ string,
      404+ unknown
      405+ >
      406+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟠 Major

      Unsafe type casting after failed JSON parse.

      When JSON parsing fails, the code casts toolCall.function.arguments to Record<string, unknown>, but if arguments is a string (which it likely is if parsing failed), this cast is incorrect and could cause runtime errors when Gemini processes the malformed args object.

      Consider logging a warning or throwing an error instead:

                let parsedArgs: Record<string, unknown> = {}
                try {
                  parsedArgs = toolCall.function.arguments
                    ? (JSON.parse(toolCall.function.arguments) as Record<
                        string,
                        unknown
                      >)
                    : {}
                } catch {
      -           parsedArgs = toolCall.function.arguments as unknown as Record<
      -             string,
      -             unknown
      -           >
      +           console.warn(
      +             `Failed to parse tool call arguments for ${toolCall.function.name}:`,
      +             toolCall.function.arguments,
      +           )
      +           // Skip this tool call or use empty args
      +           parsedArgs = {}
                }
      🤖 Prompt for AI Agents
      In packages/typescript/ai-gemini/src/adapters/text.ts around lines 393 to 406,
      the catch block unsafely casts toolCall.function.arguments to Record<string,
      unknown> after a JSON.parse failure (likely leaving a string) which can break
      downstream code; change the catch to handle the parse failure safely by: detect
      the runtime type of toolCall.function.arguments, if it's a string log a warning
      (or record a structured error) and either leave parsedArgs as an empty object or
      attempt a safer recovery (e.g., wrap the raw string under a named field like {
      raw: "<original-string>" }); alternatively throw a descriptive error if
      malformed args must be fatal — ensure the chosen behavior is logged and does not
      perform an invalid type cast.
      @@ -0,0 +354,21 @@
      354+
      355+ private mapCommonOptionsToOllama(options: ChatOptions): ChatRequest {
      356+ const model = options.model || this.defaultModel
      357+ const providerOptions = options.providerOptions as
      358+ | OllamaTextProviderOptions
      359+ | undefined
      360+
      361+ const ollamaOptions = {
      362+ temperature: options.options?.temperature,
      363+ top_p: options.options?.topP,
      364+ num_predict: options.options?.maxTokens,
      365+ ...providerOptions,
      366+ }
      367+
      368+ return {
      369+ model,
      370+ options: ollamaOptions,
      371+ messages: this.formatMessages(options.messages),
      372+ tools: this.convertToolsToOllamaFormat(options.tools),
      373+ }
      374+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟠 Major

      🧩 Analysis chain

      🏁 Script executed:

      #!/bin/bash
      # Check Ollama Message type for system role
      rg -n "role.*system" --type=ts packages/typescript/ai-ollama/

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      #!/bin/bash
      # Check the full text adapter implementation and related types
      fd "text.ts" packages/typescript/ai-ollama/src/adapters/ | head -20

      Repository: TanStack/ai

      Length of output: 105


      🏁 Script executed:

      #!/bin/bash
      # Search for systemPrompts usage in adapters to compare with other adapters
      rg -n "systemPrompts" --type=ts packages/typescript/ai-*/src/adapters/

      Repository: TanStack/ai

      Length of output: 388


      🏁 Script executed:

      #!/bin/bash
      # Check the ChatRequest and ChatOptions interfaces
      rg -n "interface ChatRequest|type ChatRequest|interface ChatOptions|type ChatOptions" --type=ts packages/typescript/ai-ollama/

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      #!/bin/bash
      # Search for Ollama message and chat type definitions
      rg -n "type.*Message|interface.*Message" --type=ts packages/typescript/ai-ollama/src/

      Repository: TanStack/ai

      Length of output: 159


      🏁 Script executed:

      #!/bin/bash
      # Check the full text.ts file to see how messages are formatted and what Ollama expects
      wc -l packages/typescript/ai-ollama/src/adapters/text.ts

      Repository: TanStack/ai

      Length of output: 109


      🏁 Script executed:

      #!/bin/bash
      # Search for formatMessages implementation
      rg -n "formatMessages" --type=ts packages/typescript/ai-ollama/src/adapters/text.ts -A 10

      Repository: TanStack/ai

      Length of output: 819


      🏁 Script executed:

      #!/bin/bash
      # Check Ollama's ChatRequest and related types
      rg -n "ChatRequest|OllamaMessage" --type=ts packages/typescript/ai-ollama/src/

      Repository: TanStack/ai

      Length of output: 633


      🏁 Script executed:

      #!/bin/bash
      # Check Message type definition
      rg -n "export.*Message|type Message" --type=ts packages/typescript/ai-ollama/src/message-types.ts -A 5

      Repository: TanStack/ai

      Length of output: 247


      🏁 Script executed:

      #!/bin/bash
      # Get the Message type/interface definition more thoroughly
      sed -n '1,100p' packages/typescript/ai-ollama/src/message-types.ts

      Repository: TanStack/ai

      Length of output: 1611


      🏁 Script executed:

      #!/bin/bash
      # Check ChatRequest type definition - look in message-types.ts
      rg -n "ChatRequest" --type=ts packages/typescript/ai-ollama/src/message-types.ts -B 2 -A 10

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      #!/bin/bash
      # Check the ollama-adapter.ts mapCommonOptionsToOllama for comparison
      sed -n '456,490p' packages/typescript/ai-ollama/src/ollama-adapter.ts

      Repository: TanStack/ai

      Length of output: 1039


      🏁 Script executed:

      #!/bin/bash
      # Check imports in text.ts to see where ChatRequest and Message come from
      head -30 packages/typescript/ai-ollama/src/adapters/text.ts

      Repository: TanStack/ai

      Length of output: 756


      🏁 Script executed:

      #!/bin/bash
      # Search for ChatRequest type definition across all ai packages
      rg -n "type ChatRequest|interface ChatRequest" --type=ts packages/typescript/

      Repository: TanStack/ai

      Length of output: 136


      🏁 Script executed:

      #!/bin/bash
      # Search for Message type definition (likely in ai-core or ollama)
      rg -n "export.*type Message|export.*interface Message" --type=ts packages/typescript/ai-ollama/src/

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      #!/bin/bash
      # Search for ollama package information - check if it's in package.json and what version
      rg -n "ollama" packages/typescript/ai-ollama/package.json

      Repository: TanStack/ai

      Length of output: 185


      🏁 Script executed:

      #!/bin/bash
      # Try to find if there are type definitions or documentation about ollama Message type
      find packages/typescript/ai-ollama -name "*.d.ts" -o -name "*.ts" | xargs grep -l "Message.*role" 2>/dev/null | head -5

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      #!/bin/bash
      # Check how other adapters (like openai, anthropic) handle system prompts vs what Ollama does
      rg -n "system" --type=ts packages/typescript/ai-anthropic/src/adapters/text.ts -B 2 -A 2 | head -40

      Repository: TanStack/ai

      Length of output: 379


      🌐 Web query:

      Ollama API system message role support chat request

      💡 Result:

      Yes — Ollama's chat API accepts a "system" role in the messages array. The message object’s role can be "system", "user", "assistant" (and "tool" for tool-enabled flows). See the API docs for the POST /api/chat endpoint. [1][2]

      Sources:

      • Ollama docs — Generate a chat message (POST /api/chat). [1]
      • Ollama API overview (messages/role description). [2]

      🏁 Script executed:

      #!/bin/bash
      # Check if there are other adapters that prepend system messages to the messages array
      rg -n "role.*system|'system'" --type=ts packages/typescript/ai-*/src/adapters/text.ts -B 3 -A 3

      Repository: TanStack/ai

      Length of output: 611


      🏁 Script executed:

      #!/bin/bash
      # Look for Ollama types more carefully - check if it's defined elsewhere or in the ollama package
      rg -n "system.*role|role.*system" --type=ts packages/typescript/ai-ollama/src/ -i

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      #!/bin/bash
      # Check the exact structure being returned in mapCommonOptionsToOllama - the full function including return
      sed -n '355,374p' packages/typescript/ai-ollama/src/adapters/text.ts

      Repository: TanStack/ai

      Length of output: 662


      🏁 Script executed:

      #!/bin/bash
      # Search for any existing system prompt handling in the Ollama adapter
      rg -n "systemPrompts" --type=ts packages/typescript/ai-ollama/src/

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      #!/bin/bash
      # Look at the full formatMessages implementation to understand message structure
      sed -n '291,320p' packages/typescript/ai-ollama/src/adapters/text.ts

      Repository: TanStack/ai

      Length of output: 1046


      🏁 Script executed:

      #!/bin/bash
      # Search for how messages are constructed elsewhere in the codebase
      rg -n "role:" --type=ts packages/typescript/ai-ollama/src/ -B 1 -A 1

      Repository: TanStack/ai

      Length of output: 1075


      🏁 Script executed:

      #!/bin/bash
      # Check the ChatResponse or message examples in the adapter
      rg -n "role\|content" --type=ts packages/typescript/ai-ollama/src/adapters/text.ts | head -20

      Repository: TanStack/ai

      Length of output: 37


      systemPrompts not forwarded to Ollama.

      The mapCommonOptionsToOllama function doesn't include options.systemPrompts in the returned ChatRequest. Unlike other adapters (OpenAI, Anthropic, Gemini) that handle system prompts, this adapter drops them entirely. Ollama's API supports system role messages, so system prompts should be prepended to the messages array.

         private mapCommonOptionsToOllama(options: ChatOptions): ChatRequest {
           const model = options.model || this.defaultModel
           const providerOptions = options.providerOptions as
             | OllamaTextProviderOptions
             | undefined
      
           const ollamaOptions = {
             temperature: options.options?.temperature,
             top_p: options.options?.topP,
             num_predict: options.options?.maxTokens,
             ...providerOptions,
           }
      
      +    // Prepend system message if systemPrompts provided
      +    const messagesWithSystem = options.systemPrompts?.length
      +      ? [
      +          { role: 'system' as const, content: options.systemPrompts.join('\n') },
      +          ...this.formatMessages(options.messages),
      +        ]
      +      : this.formatMessages(options.messages)
      +
           return {
             model,
             options: ollamaOptions,
      -      messages: this.formatMessages(options.messages),
      +      messages: messagesWithSystem,
             tools: this.convertToolsToOllamaFormat(options.tools),
           }
         }
      📝 Committable suggestion

      ‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

      Suggested change
      355- private mapCommonOptionsToOllama(options: ChatOptions): ChatRequest {
      356- const model = options.model || this.defaultModel
      357- const providerOptions = options.providerOptions as
      358- | OllamaTextProviderOptions
      359- | undefined
      360-
      361- const ollamaOptions = {
      362- temperature: options.options?.temperature,
      363- top_p: options.options?.topP,
      364- num_predict: options.options?.maxTokens,
      365- ...providerOptions,
      366- }
      367-
      368- return {
      369- model,
      370- options: ollamaOptions,
      371- messages: this.formatMessages(options.messages),
      372- tools: this.convertToolsToOllamaFormat(options.tools),
      373- }
      374- }
      355+ private mapCommonOptionsToOllama(options: ChatOptions): ChatRequest {
      356+ const model = options.model || this.defaultModel
      357+ const providerOptions = options.providerOptions as
      358+ | OllamaTextProviderOptions
      359+ | undefined
      360+
      361+ const ollamaOptions = {
      362+ temperature: options.options?.temperature,
      363+ top_p: options.options?.topP,
      364+ num_predict: options.options?.maxTokens,
      365+ ...providerOptions,
      366+ }
      367+
      368+ // Prepend system message if systemPrompts provided
      369+ const messagesWithSystem = options.systemPrompts?.length
      370+ ? [
      371+ { role: 'system' as const, content: options.systemPrompts.join('\n') },
      372+ ...this.formatMessages(options.messages),
      373+ ]
      374+ : this.formatMessages(options.messages)
      375+
      376+ return {
      377+ model,
      378+ options: ollamaOptions,
      379+ messages: messagesWithSystem,
      380+ tools: this.convertToolsToOllamaFormat(options.tools),
      381+ }
      382+ }
      🤖 Prompt for AI Agents
      In packages/typescript/ai-ollama/src/adapters/text.ts around lines 355 to 374,
      the function mapCommonOptionsToOllama drops options.systemPrompts; update it to
      prepend any provided systemPrompts (properly formatted) to the messages array
      before assigning to the ChatRequest. Specifically, call this.formatMessages on
      options.systemPrompts (if present) and concat/prepend those entries to
      this.formatMessages(options.messages), ensuring null/undefined checks so
      messages is always an array, then return the combined messages in the
      ChatRequest.
      @@ -0,0 +24,21 @@
      24+ BaseSummarizeAdapter,
      25+ type SummarizeAdapter,
      26+ type SummarizeAdapterConfig,
      27+} from './base-summarize-adapter'
      28+
      29+// Base image adapter
      30+export {
      31+ BaseImageAdapter,
      32+ type ImageAdapter,
      33+ type ImageAdapterConfig,
      34+} from './base-image-adapter'
      35+
      36+// Union type of all adapter kinds
      37+export type AdapterKind = 'chat' | 'embedding' | 'summarize' | 'image'
      38+
      39+// Union type of all adapters
      40+export type AnyAdapter =
      41+ | ChatAdapter<any, any, any, any, any>
      42+ | EmbeddingAdapter<any, any>
      43+ | SummarizeAdapter<any, any>
      44+ | ImageAdapter<any, any, any>
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🔴 Critical

      🧩 Analysis chain

      🏁 Script executed:

      rg -n "export interface ImageAdapter<" --type=ts -A 10 packages/typescript/ai/src/adapters/base-image-adapter.ts

      Repository: TanStack/ai

      Length of output: 614


      Update ImageAdapter in AnyAdapter union to include missing 4th generic parameter.

      ImageAdapter has 4 generic type parameters (TModels, TProviderOptions, TModelProviderOptionsByName, TModelSizeByName), but is currently defined in the AnyAdapter union with only 3 type arguments. Update line 44 to: ImageAdapter<any, any, any, any>

      🤖 Prompt for AI Agents
      In packages/typescript/ai/src/adapters/index.ts around lines 39 to 44, the
      AnyAdapter union lists ImageAdapter with only three generic parameters; update
      that union member to provide the missing fourth generic (use ImageAdapter<any,
      any, any, any>) so the type signature matches the ImageAdapter definition and
      resolves the generic arity mismatch.
      packages/typescript/ai/src/core/generate.ts lines 525 to 540OutdatedResolvedby AlemTuzlak
      @@ -731,0 +775,21 @@
      775+ url?: string
      776+ /** Revised prompt used by the model (if applicable) */
      777+ revisedPrompt?: string
      778+}
      779+
      780+/**
      781+ * Result of image generation
      782+ */
      783+export interface ImageGenerationResult {
      784+ /** Unique identifier for the generation */
      785+ id: string
      786+ /** Model used for generation */
      787+ model: string
      788+ /** Array of generated images */
      789+ images: Array<GeneratedImage>
      790+ /** Token usage information (if available) */
      791+ usage?: {
      792+ inputTokens?: number
      793+ outputTokens?: number
      794+ totalTokens?: number
      795+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      Standardize token usage field naming.

      The usage object in ImageGenerationResult uses inputTokens and outputTokens, while other result types use different naming:

      • DoneStreamChunk (line 644): uses promptTokens and completionTokens
      • EmbeddingResult (line 740): uses promptTokens

      Consider standardizing to one naming convention across all usage types for consistency.

      Apply this diff to align with existing conventions:

         /** Token usage information (if available) */
         usage?: {
      -    inputTokens?: number
      -    outputTokens?: number
      +    promptTokens?: number
      +    completionTokens?: number
           totalTokens?: number
         }
      🤖 Prompt for AI Agents
      packages/typescript/ai/src/types.ts around lines 791 to 795: the
      ImageGenerationResult usage object currently uses inputTokens/outputTokens which
      is inconsistent with other types that use promptTokens and completionTokens;
      rename usage.inputTokens -> usage.promptTokens and usage.outputTokens ->
      usage.completionTokens (keep totalTokens) and update any related type references
      and consumers/serializers/deserializers to use the new field names to maintain
      consistency across result types.
    7. Actionable comments posted: 0

      ♻️ Duplicate comments (5)
      packages/typescript/ai-anthropic/src/adapters/text.ts (5)

      92-121: Avoid emitting error.code as the literal string "undefined" in chatStream

      If both err.code and err.status are absent, String(err.status) becomes "undefined", which is not useful to consumers.

      Consider a safer fallback:

             yield {
               type: 'error',
               id: generateId(this.name),
               model: options.model,
               timestamp: Date.now(),
               error: {
                 message: err.message || 'Unknown error occurred',
      -          code: err.code || String(err.status),
      +          code:
      +            err.code ??
      +            (err.status != null ? String(err.status) : 'unknown'),
               },
             }

      129-178: Structured output silently returns data: null on JSON parse failure

      In structuredOutput, a JSON.parse error is caught and coerced to parsed = null, so callers just see data: null with no indication that parsing failed. This can mask real schema/format issues.

      Suggest failing fast with a descriptive error (or otherwise surfacing parse failure):

      -      let parsed: unknown = null
      -      try {
      -        parsed = JSON.parse(text)
      -      } catch {
      -        parsed = null
      -      }
      +      let parsed: unknown
      +      try {
      +        parsed = JSON.parse(text)
      +      } catch (err) {
      +        throw new Error(
      +          `Failed to parse structured output as JSON: ${
      +            err instanceof Error ? err.message : String(err)
      +          }`,
      +        )
      +      }

      This will then be wrapped by the outer catch and surfaced as a structured output generation error instead of a silent null.


      181-242: Clarify precedence between systemPrompts and providerOptions.system

      validProviderOptions includes 'system' and is spread after systemPrompts:

      system: options.systemPrompts?.join('\n'),
      tools: tools,
      ...validProviderOptions,

      This means a providerOptions.system value will override the higher‑level systemPrompts, which is easy to miss and currently undocumented.

      If you want systemPrompts to always win (which is what most consumers will expect), either:

      • Remove 'system' from validKeys, or
      • Spread validProviderOptions before the explicit system field:
      -    const requestParams: InternalTextProviderOptions = {
      -      model: options.model,
      -      max_tokens: maxTokens,
      -      temperature: options.options?.temperature,
      -      top_p: options.options?.topP,
      -      messages: formattedMessages,
      -      system: options.systemPrompts?.join('\n'),
      -      tools: tools,
      -      ...validProviderOptions,
      -    }
      +    const requestParams: InternalTextProviderOptions = {
      +      model: options.model,
      +      max_tokens: maxTokens,
      +      temperature: options.options?.temperature,
      +      top_p: options.options?.topP,
      +      messages: formattedMessages,
      +      ...validProviderOptions,
      +      system: options.systemPrompts?.join('\n'),
      +      tools: tools,
      +    }

      If provider‑level override is intentional, it would help to document and add a test capturing that precedence.


      408-424: content_block_* handling should use the event index instead of a global currentToolIndex

      processAnthropicStream tracks tool calls via a currentToolIndex counter that increments on each content_block_start of type tool_use and is then reused for input_json_delta and content_block_stop. However, Anthropic’s streaming events already include an index field on content_block_start, content_block_delta, and content_block_stop.

      With mixed content blocks (e.g., text, thinking, tool_use) the current approach can misassociate input_json_delta/content_block_stop events with the wrong tool, leading to spurious or mis‑indexed tool_call chunks.

      You can avoid this by keying off event.index instead of a mutable currentToolIndex:

      -    const toolCallsMap = new Map<
      -      number,
      -      { id: string; name: string; input: string }
      -    >()
      -    let currentToolIndex = -1
      +    const toolCallsMap = new Map<
      +      number,
      +      { id: string; name: string; input: string }
      +    >()
      
      @@
      -        if (event.type === 'content_block_start') {
      -          if (event.content_block.type === 'tool_use') {
      -            currentToolIndex++
      -            toolCallsMap.set(currentToolIndex, {
      +        if (event.type === 'content_block_start') {
      +          if (event.content_block.type === 'tool_use') {
      +            toolCallsMap.set(event.index, {
                     id: event.content_block.id,
                     name: event.content_block.name,
                     input: '',
                   })
      @@
      -          } else if (event.delta.type === 'input_json_delta') {
      -            const existing = toolCallsMap.get(currentToolIndex)
      +          } else if (event.delta.type === 'input_json_delta') {
      +            const existing = toolCallsMap.get(event.index)
                   if (existing) {
                     existing.input += event.delta.partial_json
      @@
      -              index: currentToolIndex,
      +              index: event.index,
                   }
                 }
               }
      -        } else if (event.type === 'content_block_stop') {
      -          const existing = toolCallsMap.get(currentToolIndex)
      +        } else if (event.type === 'content_block_stop') {
      +          const existing = toolCallsMap.get(event.index)
                 if (existing && existing.input === '') {
      @@
      -              index: currentToolIndex,
      +              index: event.index,
                   }
                 }
               }

      This keeps tool-call tracking aligned with Anthropic’s own block indices and avoids cross‑contamination between different content blocks.

      Also applies to: 451-471, 473-491


      554-565: Duplicate error.code "undefined" issue in stream error handler

      Same concern as in chatStream: code: err.code || String(err.status) can yield "undefined" when both are missing. Recommend aligning the fallback with the earlier suggestion:

             yield {
               type: 'error',
               id: genId(),
               model: model,
               timestamp,
               error: {
                 message: err.message || 'Unknown error occurred',
      -          code: err.code || String(err.status),
      +          code:
      +            err.code ??
      +            (err.status != null ? String(err.status) : 'unknown'),
               },
             }
      🧹 Nitpick comments (1)
      packages/typescript/ai-anthropic/src/adapters/text.ts (1)

      492-552: Make usage handling more defensive and avoid potential double done events

      Two small robustness points in the message_delta / message_stop handling:

      1. Guard event.usage access: If Anthropic ever omits usage on the final message_delta (even with stop_reason present), the current code will throw on event.usage.input_tokens. A cheap defensive improvement:
      -                  usage: {
      -                    promptTokens: event.usage.input_tokens || 0,
      -                    completionTokens: event.usage.output_tokens || 0,
      -                    totalTokens:
      -                      (event.usage.input_tokens || 0) +
      -                      (event.usage.output_tokens || 0),
      -                  },
      +                  usage: {
      +                    promptTokens: event.usage?.input_tokens ?? 0,
      +                    completionTokens: event.usage?.output_tokens ?? 0,
      +                    totalTokens:
      +                      (event.usage?.input_tokens ?? 0) +
      +                      (event.usage?.output_tokens ?? 0),
      +                  },
      1. Consider emitting a single done event per stream: Depending on Anthropic’s exact event sequence, you might get both a message_delta with stop_reason and a later message_stop, which will currently yield two done events. If the consumer expects a single terminal chunk, you could track a let doneEmitted = false and skip subsequent done emissions once set.

      This is more of a resiliency/clarity tweak than a correctness blocker.

      📜 Review details

      Configuration used: CodeRabbit UI

      Review profile: CHILL

      Plan: Pro

      📥 Commits

      Reviewing files that changed from the base of the PR and between 14c429704b36287aeb26d592b821948160ebdf0b and 935111d68e72f7364b93f78589bda2105832e020.

      📒 Files selected for processing (1)
      • packages/typescript/ai-anthropic/src/adapters/text.ts (1 hunks)
      🔇 Additional comments (4)
      packages/typescript/ai-anthropic/src/adapters/text.ts (4)

      47-90: Adapter config and client wiring look solid

      AnthropicTextConfig as a thin alias plus AnthropicTextAdapter extending BaseChatAdapter and initializing this.client via createAnthropicClient is straightforward and consistent with the rest of the adapters. No changes requested here.


      244-308: Content-part conversion to Anthropic blocks looks consistent

      The handling of text, image, and document parts (with metadata mapping and safe stripping of mediaType for images) and the explicit rejection of audio/video is clear and matches Anthropic’s capabilities. The exhaustive never check on the default branch is a nice type‑safety touch.


      310-398: Message formatting for tools and multi-part user content looks reasonable

      The mapping logic:

      • Converts role: 'tool' + toolCallId into Anthropic tool_result blocks on a user message.
      • Packages assistant tool calls as tool_use blocks (with best‑effort JSON parsing of function.arguments).
      • Properly converts multi‑part user content via convertContentPartToAnthropic.
      • Falls back to string or converted content array in other cases.

      This matches the usual TanStack AI message‑to‑provider mapping; I don’t see any correctness issues here.


      574-589: Factory helpers are thin and ergonomic

      createAnthropicText and anthropicText are simple, typed helpers that mirror the other adapters’ factory pattern and keep env‑key detection nicely encapsulated. No issues from my side.

    8. Actionable comments posted: 19

      Caution

      Some comments are outside the diff and can’t be posted inline due to platform limitations.

      ⚠️ Outside diff range comments (14)
      docs/reference/functions/textOptions.md (1)

      6-13: Fix incorrect "Defined in" link—function is in activities/text/index.ts:1211, not utilities/chat-options.ts.

      The documented source location is wrong. textOptions is exported from packages/typescript/ai/src/activities/text/index.ts starting at line 1211, not from a non-existent utilities/chat-options.ts file. Update the link accordingly.

      docs/guides/tool-approval.md (1)

      58-74: Fix the adapter function name in the code snippet: use openai() instead of openaiText().

      The TanStack AI package exports openai from @tanstack/ai-openai, not openaiText(). The corrected snippet should be:

      import { ai, toStreamResponse } from "@tanstack/ai";
      import { openai } from "@tanstack/ai-openai";
      import { sendEmail } from "./tools";
      
      export async function POST(request: Request) {
        const { messages } = await request.json();
      
        const stream = ai({
          adapter: openai(),
          messages,
          model: "gpt-4o",
          tools: [sendEmail],
        });
      
        return toStreamResponse(stream);
      }

      The openai() adapter defaults to reading OPENAI_API_KEY from environment variables (server-side only). If docs need to clarify auth setup, consider adding a note that the environment variable must be set before deployment.

      examples/ts-group-chat/chat-server/capnweb-rpc.ts (1)

      154-199: Bug risk: joinChat() registers clients, but leaveChat() doesn’t unregister—stale “connected users” likely. This can keep usernames in clients (and queues) after a user leaves without closing the socket. Consider cleaning up in leaveChat() (and possibly also handling duplicate usernames in joinChat()).

         async leaveChat() {
           if (!this.currentUsername) return
       
      -    console.log(`${this.currentUsername} is leaving the chat`)
      -    await globalChat.removeUser(this.currentUsername)
      -    this.currentUsername = null
      +    const username = this.currentUsername
      +    console.log(`${username} is leaving the chat`)
      +    await globalChat.removeUser(username)
      +    clients.delete(username)
      +    userMessageQueues.delete(username)
      +    this.currentUsername = null
       
           return {
             message: 'Successfully left the chat',
           }
         }
      examples/ts-svelte-chat/src/routes/api/chat/+server.ts (1)

      105-119: Remove as any type casts to preserve type safety.

      The migration to ai() is correct, but lines 106-107 use as any to bypass type checking. This defeats TypeScript's type safety and could mask incompatibilities between the text adapters and the ai() function.

      Consider investigating the type mismatch. Possible solutions:

      1. If the adapter types are narrower than ai() expects, update the ai() function signature to accept the union of text adapter types
      2. If the types should already be compatible, refine the switch statement's type inference or use explicit type annotations
      3. If types are still being refined in this PR, add a TODO comment indicating this is temporary

      Apply this approach to eliminate the type casts:

       const stream = ai({
      -  adapter: adapter as any,
      -  model: selectedModel as any,
      +  adapter,
      +  model: selectedModel,
         tools: [
           getGuitars,
           recommendGuitarToolDef,

      If type errors arise, they will guide you to the proper type definitions needed.

      docs/adapters/ollama.md (1)

      98-124: Docs examples use messages without defining it In “With Tools” and “Provider Options” examples, messages isn’t declared in-snippet, which makes copy/paste fail.

       import { ai, toolDefinition } from "@tanstack/ai";
       import { ollamaText } from "@tanstack/ai-ollama";
       import { z } from "zod";
      
       const adapter = ollamaText();
      +const messages = [{ role: "user", content: "Hello!" }];
      
       const getLocalDataDef = toolDefinition({
         name: "get_local_data",
         description: "Get data from local storage",
         inputSchema: z.object({
           key: z.string(),
         }),
       });

      Also applies to: 132-147

      examples/README.md (2)

      311-321: Inconsistent adapter import - should use openaiText for consistency.

      The import on line 312 uses the legacy openai adapter, but per the PR objectives, the tree-shakeable openaiText adapter should be used instead to align with the new API surface demonstrated elsewhere.

       import { ai, toStreamResponse } from '@tanstack/ai'
      -import { openai } from '@tanstack/ai-openai'
      +import { openaiText } from '@tanstack/ai-openai'
       
       const stream = ai({
      -  adapter: openai(),
      +  adapter: openaiText(),
         model: 'gpt-4o',
         messages,
       })

      385-391: Inconsistent API call - should use ai() instead of chat().

      This example still uses the old chat({...}) function call, but should be updated to ai({...}) to match the PR's migration pattern.

      -const stream = chat({
      +const stream = ai({
         adapter: openai(),
         model: 'gpt-4o',
         messages,
         tools: [weatherTool], // SDK executes these automatically
       })
      docs/reference/type-aliases/TextStreamOptionsUnion.md (1)

      1-18: Fix incorrect source file line reference.

      The documentation references line 823, but TextStreamOptionsUnion is actually defined at line 890 in packages/typescript/ai/src/types.ts. Update the link to point to the correct line.

      docs/getting-started/quick-start.md (1)

      179-194: Tools snippet calls ai(...) without importing it.

      -import { toolDefinition } from '@tanstack/ai'
      +import { ai, toolDefinition } from '@tanstack/ai'
      docs/guides/client-tools.md (1)

      235-239: Tool state name completed may be inconsistent with other docs/examples (complete) — please align to the actual enum values.

      docs/guides/tools.md (1)

      282-288: Hybrid tools example uses two tools: keys in one object literal (invalid / misleading). Split into two separate examples.

      -ai({
      -  adapter: openaiText(),
      -  messages,
      -  tools: [addToCartDef], // Client will execute, or
      -  tools: [addToCartServer], // Server will execute
      -});
      +// Client will execute (pass definition)
      +ai({ adapter: openaiText(), messages, tools: [addToCartDef] });
      +
      +// Server will execute (pass server implementation)
      +ai({ adapter: openaiText(), messages, tools: [addToCartServer] });
      docs/api/ai.md (2)

      47-61: Inconsistent section header with actual API usage.

      The section header says summarize(options) but the code example uses ai({ adapter: openaiSummarize(), ... }). Either update the header to reflect the unified ai() entry point or clarify that summarize() is a separate function.

      -## `summarize(options)`
      +## Summarization with `ai()`
       
      -Creates a text summarization.
      +Creates a text summarization using an adapter.

      76-89: Inconsistent section header with actual API usage.

      Similar to the summarize section, the header says embedding(options) but the code uses ai({ adapter: openaiEmbed(), ... }).

      -## `embedding(options)`
      +## Embeddings with `ai()`
       
      -Creates embeddings for text input.
      +Creates embeddings for text input using an adapter.
      packages/typescript/ai/src/index.ts (1)

      74-98: Leaky public types: exporting InternalToolCallState from the root likely locks you into it. Given the “Internal…” name, either (a) don’t export it from src/index.ts, or (b) mark it /** @internal */ and ensure the build actually strips/flags internal types consistently.

      ♻️ Duplicate comments (17)
      examples/ts-group-chat/chat-server/chat-logic.ts (1)

      108-108: Same array syntax consideration applies to method return types.

      The same Type[] → Array<Type> conversion appears in these method signatures, consistent with the interface changes above.

      Also applies to: 112-112

      packages/typescript/ai-ollama/src/adapters/text.ts (2)

      199-222: Critical: Ollama's ToolCall lacks id property—type assertion hides runtime issue.

      According to Ollama's v0.6.3 interface definition, ToolCall only contains function.name and function.arguments. The type assertion on lines 200-203 bypasses TypeScript's type checking but doesn't create the id property at runtime. Line 211 will access an undefined actualToolCall.id.

      Use responseId as the tool call identifier instead:

       const handleToolCall = (toolCall: ToolCall): StreamChunk => {
      -  const actualToolCall = toolCall as ToolCall & {
      -    id: string
      -    function: { index: number }
      -  }
         return {
           type: 'tool_call',
           id: responseId,
           model: chunk.model,
           timestamp,
           toolCall: {
             type: 'function',
      -      id: actualToolCall.id,
      +      id: `${responseId}-tool-${Math.random().toString(36).substring(7)}`,
             function: {
      -        name: actualToolCall.function.name || '',
      +        name: toolCall.function.name || '',
               arguments:
      -          typeof actualToolCall.function.arguments === 'string'
      -            ? actualToolCall.function.arguments
      -            : JSON.stringify(actualToolCall.function.arguments),
      +          typeof toolCall.function.arguments === 'string'
      +            ? toolCall.function.arguments
      +            : JSON.stringify(toolCall.function.arguments),
             },
           },
      -    index: actualToolCall.function.index,
      +    index: 0,
         }
       }

      366-385: systemPrompts not forwarded to Ollama.

      The mapCommonOptionsToOllama function doesn't include options.systemPrompts in the returned ChatRequest. Other adapters (OpenAI, Anthropic, Gemini) prepend system prompts to the messages array. Ollama's API supports system role messages, so system prompts should be handled.

      Prepend system messages when systemPrompts is provided:

       private mapCommonOptionsToOllama(options: TextOptions): ChatRequest {
         const model = options.model || this.defaultModel
         const providerOptions = options.providerOptions as
           | OllamaTextProviderOptions
           | undefined
      
         const ollamaOptions = {
           temperature: options.options?.temperature,
           top_p: options.options?.topP,
           num_predict: options.options?.maxTokens,
           ...providerOptions,
         }
      
      +  // Prepend system message if systemPrompts provided
      +  const messagesWithSystem = options.systemPrompts?.length
      +    ? [
      +        { role: 'system' as const, content: options.systemPrompts.join('\n') },
      +        ...this.formatMessages(options.messages),
      +      ]
      +    : this.formatMessages(options.messages)
      +
         return {
           model,
           options: ollamaOptions,
      -    messages: this.formatMessages(options.messages),
      +    messages: messagesWithSystem,
           tools: this.convertToolsToOllamaFormat(options.tools),
         }
       }
      packages/typescript/ai-gemini/src/adapters/text.ts (6)

      256-267: Remove dead chunk.data handling (likely not a real field on streamed Gemini responses). This branch will either never run or mask future shape mismatches.


      205-255: Tool call IDs can collide; always generate a unique toolCallId (don’t use functionCall.name as the id). Same function can be called multiple times in a run; using the name as the id breaks correlation.

      -            const toolCallId =
      -              functionCall.name || `call_${Date.now()}_${nextToolIndex}`
      +            const toolCallId = `call_${Date.now()}_${nextToolIndex}`
                   const functionArgs = functionCall.args || {}
      @@
      -                const toolCallId =
      -                  functionCall.name || `call_${Date.now()}_${nextToolIndex}`
      +                const toolCallId = `call_${Date.now()}_${nextToolIndex}`
                       const functionArgs = functionCall.args || {}

      Also applies to: 272-311


      325-338: Token accounting: completionTokens should not come from thoughtsTokenCount. thoughtsTokenCount is reasoning/thought tokens; completion text tokens should come from the response candidate tokens field.

      Gemini `usageMetadata` fields: what do `candidatesTokenCount`, `promptTokenCount`, `totalTokenCount`, and `thoughtsTokenCount` mean, and which corresponds to completion/output tokens for billing/usage reporting?
      -                completionTokens: chunk.usageMetadata.thoughtsTokenCount ?? 0,
      +                completionTokens: chunk.usageMetadata.candidatesTokenCount ?? 0,

      343-372: MIME type fallback is incorrect for non-image modalities. Defaulting everything to 'image/jpeg' will break audio/video/document uploads.

      -              mimeType: metadata?.mimeType ?? 'image/jpeg',
      +              mimeType: metadata?.mimeType ?? 'application/octet-stream',
      @@
      -              mimeType: metadata?.mimeType ?? 'image/jpeg',
      +              mimeType: metadata?.mimeType ?? 'application/octet-stream',

      397-412: Don’t cast to Record<string, unknown> after JSON parse failure—recover safely. A failed parse almost certainly means you have a string; the current cast risks runtime errors downstream.

                 } catch {
      -            parsedArgs = toolCall.function.arguments as unknown as Record<
      -              string,
      -              unknown
      -            >
      +            parsedArgs = {}
                 }

      423-432: functionResponse.name must be the tool/function name, not toolCallId. Gemini expects the declared function name here; using an ID can break tool-result correlation.

      Gemini function calling: what must `functionResponse.name` be set to when returning tool results (function name vs call id), and how is correlation handled?
      -          functionResponse: {
      -            name: msg.toolCallId,
      +          functionResponse: {
      +            name: msg.name ?? '',
                   response: {
                     content: msg.content || '',
                   },
                 },
      packages/typescript/ai-openai/src/adapters/text.ts (2)

      226-227: Either populate eventTypeCounts or remove it (currently it always logs empty counts). Right now the log implies useful diagnostics, but nothing ever increments the map.

      -    const eventTypeCounts = new Map<string, number>()
      +    const eventTypeCounts = new Map<string, number>()
      @@
             for await (const chunk of stream) {
      +        eventTypeCounts.set(chunk.type, (eventTypeCounts.get(chunk.type) ?? 0) + 1)
               chunkCount++

      Also applies to: 479-486


      654-668: Base64 image inputs must be converted to a data URI for image_url (currently passing raw base64). part.source.value for source.type === 'data' is base64, not a URL.

      OpenAI Responses API `input_image.image_url`: does it accept raw base64, or must it be a URL / `data:<mime>;base64,<...>` data URI?
             case 'image': {
               const imageMetadata = part.metadata
               if (part.source.type === 'url') {
                 return {
                   type: 'input_image',
                   image_url: part.source.value,
                   detail: imageMetadata?.detail || 'auto',
                 }
               }
      -        // For base64 data, construct a data URI
      +        // For base64 data, construct a data URI
               return {
                 type: 'input_image',
      -          image_url: part.source.value,
      +          image_url: `data:image/jpeg;base64,${part.source.value}`,
                 detail: imageMetadata?.detail || 'auto',
               }
             }
      packages/typescript/ai/src/types.ts (2)

      572-578: Clarify relationship between outputSchema and output fields.

      This issue was flagged in a previous review. The TextOptions interface now has both output?: TOutput (line 571) and outputSchema?: z.ZodType (line 578). Consider adding documentation clarifying precedence rules or runtime validation.


      794-798: Token usage field naming inconsistency.

      This issue was flagged in a previous review. The usage object in ImageGenerationResult uses inputTokens/outputTokens, while other result types use promptTokens/completionTokens. Consider standardizing the naming convention.

      packages/typescript/ai-anthropic/src/adapters/text.ts (4)

      117-120: Error code may result in string "undefined".

      This issue was flagged in a previous review. If both err.code and err.status are undefined, String(err.status) produces "undefined" as the error code.


      261-270: Spread of validProviderOptions can override systemPrompts.

      This issue was flagged in a previous review. The system field is set explicitly at line 267, but since 'system' is included in validKeys (line 232), it can be extracted from providerOptions and will override the joined systemPrompts when spread at line 269.


      504-522: Use the index field from content_block_stop to identify which block actually stopped.

      This issue was flagged in a previous review. The handler checks toolCallsMap.get(currentToolIndex) without verifying that the stopped block is actually a tool_use block using the event's index field.


      593-596: Duplicate error code issue.

      This issue was flagged in a previous review. Same issue as in chatStream: if both err.code and err.status are undefined, the code will be "undefined".

      🧹 Nitpick comments (33)
      docs/reference/functions/textOptions.md (1)

      8-10: Doc generics: TModel appears unused in the shown signature—either show its role or drop it. The snippet renders function textOptions<TAdapter, TModel>(options): ...TextStreamOptionsUnion<TAdapter>... (Line 9), but TModel isn’t referenced anywhere in the displayed types (Line 9, 20-22). If TModel is actually part of the real signature/constraints, consider reflecting that in the snippet/parameter type; otherwise, removing it would reduce confusion.

      Also applies to: 14-23

      docs/guides/tool-approval.md (1)

      63-74: Consider a one-line note about auth/config expectations for openaiText() in server environments. Right now the example will fail silently for readers unless they already know where the OpenAI API key is sourced from; a short note like “ensure your OpenAI API key is configured (e.g., env var) before calling openaiText()” would reduce support churn.

      examples/ts-group-chat/chat-server/capnweb-rpc.ts (2)

      8-13: Prefer unknown (or a real ToolCall shape) over any for ModelMessage.toolCalls. If this is meant to model tool calls, Array<unknown> is a safer default than Array<any> until the structure is known.


      61-62: clients and broadcastToAll() look inconsistent: callbacks are registered, but “broadcast” only queues messages. If callbacks are intentional, consider invoking them (and only then counting “successful”); otherwise, consider renaming clients/removing the callback map to avoid confusion.

      Also applies to: 89-141

      packages/typescript/ai/src/activities/text/tools/zod-converter.ts (2)

      9-16: Consider the fragility of internal API usage.

      The _zod property is an internal Zod implementation detail that could change across versions. While this type guard may be necessary, be aware that it couples this code to Zod's internals and could break in future Zod updates.

      Consider:

      • Adding a comment noting this is intentionally accessing internals
      • Checking if Zod provides a public type guard or utility (e.g., checking for common Zod methods like parse, safeParse)
      • Wrapping this in a try-catch when used, if version compatibility becomes an issue

      198-201: Internal Zod API usage for object detection.

      Similar to isZodSchema, this code accesses internal Zod structure (def.type). While necessary for the edge case handling, it creates maintenance risk if Zod's internal structure changes.

      This is acceptable for now but consider consolidating all internal API checks into a single utility module with clear documentation about the version compatibility assumptions.

      packages/typescript/ai-devtools/src/store/ai-context.tsx (1)

      1309-1384: Consider subscribing to both chat:* and text:* events for backward compatibility. Right now, devtools will miss usage events if an app/core emits the older chat:* names. A small compatibility shim keeps things resilient during the transition.

      -    // ============= Chat Events (for usage tracking) =============
      +    // ============= Text Events (for usage tracking) =============
      
      -    cleanupFns.push(
      -      aiEventClient.on('text:started', (e) => {
      +    const onTextStarted = (e: any) => {
               const streamId = e.payload.streamId
               const model = e.payload.model
               const provider = e.payload.provider
               const clientId = e.payload.clientId
      @@
      -      }),
      -    )
      +    }
      +
      +    cleanupFns.push(aiEventClient.on('text:started', onTextStarted))
      +    cleanupFns.push(aiEventClient.on('chat:started', onTextStarted))
      
      -    cleanupFns.push(
      -      aiEventClient.on('text:completed', (e) => {
      +    const onTextCompleted = (e: any) => {
               const { requestId, usage } = e.payload
      @@
      -      }),
      -    )
      +    }
      +
      +    cleanupFns.push(aiEventClient.on('text:completed', onTextCompleted))
      +    cleanupFns.push(aiEventClient.on('chat:completed', onTextCompleted))
      
      -    cleanupFns.push(
      -      aiEventClient.on('text:iteration', (e) => {
      +    const onTextIteration = (e: any) => {
               const { requestId, iterationNumber } = e.payload
      @@
      -      }),
      -    )
      +    }
      +
      +    cleanupFns.push(aiEventClient.on('text:iteration', onTextIteration))
      +    cleanupFns.push(aiEventClient.on('chat:iteration', onTextIteration))
      examples/ts-group-chat/chat-server/claude-service.ts (1)

      152-158: Consider removing the as any cast on messages.

      The as any cast on line 155 bypasses type checking and may hide type mismatches between the conversation history and the expected message format. If there's a genuine type incompatibility, it would be better to address it explicitly.

      -        messages: [...conversationHistory] as any,
      +        messages: [...conversationHistory],

      If this causes a type error, the underlying type mismatch should be resolved rather than suppressed.

      examples/ts-group-chat/package.json (1)

      17-20: Minor version inconsistency between router packages.

      @tanstack/react-router is updated to ^1.141.1 while @tanstack/react-router-devtools remains at ^1.139.7. This may be intentional, but consider aligning versions if devtools compatibility is desired.

      examples/ts-solid-chat/src/routes/api.chat.ts (1)

      46-54: Consider propagating the request signal to abort controller.

      requestSignal is captured on line 47 but only used for the early abort check. Consider linking it to abortController so that if the client disconnects mid-stream, the AI request is aborted.

              const abortController = new AbortController()
      +
      +       // Abort the AI request if the client disconnects
      +       requestSignal?.addEventListener('abort', () => abortController.abort())
      packages/typescript/ai-openai/src/utils/schema-converter.ts (1)

      64-126: Nested object schemas may be skipped if type: 'object' is omitted Inside makeOpenAIStructuredOutputCompatible, recursion is gated on prop.type === 'object', but some JSON Schemas represent objects via properties without an explicit type. Consider treating “has properties” as object-like.

       function makeOpenAIStructuredOutputCompatible(
         schema: Record<string, any>,
         originalRequired: Array<string> = [],
       ): Record<string, any> {
         const result = { ...schema }
      
         // Handle object types
      -  if (result.type === 'object' && result.properties) {
      +  const isObjectLike = result.type === 'object' || (!!result.properties && result.type == null)
      +  if (isObjectLike && result.properties) {
      +    result.type = 'object'
           const properties = { ...result.properties }
           const allPropertyNames = Object.keys(properties)
      
           // Transform each property
           for (const propName of allPropertyNames) {
             const prop = properties[propName]
             const wasOptional = !originalRequired.includes(propName)
      
             // Recursively transform nested objects/arrays
      -      if (prop.type === 'object' && prop.properties) {
      +      const propIsObjectLike =
      +        prop?.type === 'object' || (!!prop?.properties && prop?.type == null)
      +      if (propIsObjectLike && prop.properties) {
               properties[propName] = makeOpenAIStructuredOutputCompatible(
                 prop,
                 prop.required || [],
               )
             } else if (prop.type === 'array' && prop.items) {
               properties[propName] = {
                 ...prop,
                 items: makeOpenAIStructuredOutputCompatible(
                   prop.items,
                   prop.items.required || [],
                 ),
               }
             } else if (wasOptional) {
               // Make optional fields nullable by adding null to the type
               if (prop.type && !Array.isArray(prop.type)) {
                 properties[propName] = {
                   ...prop,
                   type: [prop.type, 'null'],
                 }
               } else if (Array.isArray(prop.type) && !prop.type.includes('null')) {
                 properties[propName] = {
                   ...prop,
                   type: [...prop.type, 'null'],
                 }
               }
             }
           }
      packages/typescript/ai/src/activities/embedding/adapter.ts (1)

      66-68: generateId() randomness slice is inconsistent/low-quality vs other modules substring(7) can yield very short strings. Consider matching the fixed-length pattern used elsewhere (e.g. slice(2, 9)) or using a shared helper.

         protected generateId(): string {
      -    return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`
      +    return `${this.name}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`
         }
      packages/typescript/ai/src/activities/summarize/index.ts (2)

      155-181: Dispatch logic is fine, but the casts are a bit heavy Not blocking, but consider overloads to avoid the as unknown as ... casts (keeps the public API nicer to maintain).


      93-95: Consider reusing a shared createId() helper This duplicates the same logic already present in the text activity (and now embedding has its own ID generation too).

      packages/typescript/ai-ollama/src/utils/schema-converter.ts (1)

      8-15: Note: isZodSchema relies on internal Zod API.

      The _zod property check is pragmatic but relies on Zod's internal implementation. While this pattern is commonly used in the ecosystem, it could break if Zod changes its internals in future versions.

      Consider adding a comment in the function documentation noting this dependency on Zod internals, or adding a runtime test to verify the check still works as expected.

      packages/typescript/ai-gemini/src/utils/schema-converter.ts (1)

      59-84: Schema normalization uses internal Zod properties.

      The check at lines 60-63 accesses schema.def.type, which is another internal Zod property. While the defensive normalization logic ensures Gemini API compatibility, consider consolidating these checks:

         // Ensure object schemas always have type: "object"
         if (typeof result === 'object') {
      -    const isZodObject =
      -      typeof schema === 'object' &&
      -      'def' in schema &&
      -      schema.def.type === 'object'
      -
      -    if (isZodObject && !result.type) {
      -      result.type = 'object'
      -    }
      -
      -    if (Object.keys(result).length === 0) {
      -      result.type = 'object'
      -    }
      -
      -    if ('properties' in result && !result.type) {
      +    // Infer object type from properties or empty schema
      +    if (
      +      !result.type &&
      +      ('properties' in result || Object.keys(result).length === 0)
      +    ) {
             result.type = 'object'
           }
      
           if (result.type === 'object' && !('properties' in result)) {
             result.properties = {}
           }
      
           if (result.type === 'object' && !('required' in result)) {
             result.required = []
           }
         }
      examples/ts-vue-chat/vite.config.ts (1)

      234-248: Type casts (as any) are pragmatic for dynamic provider selection.

      The adapter as any and selectedModel as any casts at Lines 235-236 lose type safety but are understandable given the runtime provider switching. For a production codebase, consider a discriminated union approach, but this is acceptable for an example.

      A more type-safe approach could use a helper that returns properly typed options:

      function createAIOptions(provider: Provider, model: string | undefined, ...rest) {
        switch (provider) {
          case 'openai':
            return { adapter: openaiText(), model: model ?? 'gpt-4o', ...rest };
          // ... other cases
        }
      }
      packages/typescript/ai-ollama/src/ollama-adapter.ts (1)

      398-405: URL-based images may not work reliably with Ollama.

      The comment at Line 402-404 notes that URL-based images aren't directly supported by Ollama. Consider logging a warning or throwing an error for unsupported image sources rather than silently passing them through.

                 } else {
      -            // URL-based images not directly supported, but we pass the URL
      -            // Ollama may need the image to be fetched externally
      -            images.push(part.source.value)
      +            // URL-based images require external fetching - warn user
      +            console.warn(
      +              `Ollama: URL-based images require external fetching. Consider using base64 data instead.`
      +            )
      +            images.push(part.source.value)
                 }
      packages/typescript/ai/src/ai.ts (4)

      38-47: AnyAdapter naming is confusing next to AnyAIAdapter; consider renaming or removing the alias. Exporting both AnyAdapter and AnyAIAdapter invites accidental imports and ambiguous docs/examples. If you need backwards compat, a more explicit alias name (e.g. AnyGenerateAdapter) is safer.


      39-44: Reduce any in GenerateAdapter to preserve inference. Using any for provider/model mappings in the union makes ai() inference weaker than it needs to be; consider plumbing the generic parameters through (or using unknown + constraints) so adapter-specific option typing is retained.


      62-119: Type-level routing looks solid, but consider extracting per-kind helpers to keep this maintainable. AIOptionsFor/AIReturnFor are correct-looking but will be hard to evolve as new adapter kinds/options land; splitting into TextAIOptionsFor, EmbeddingAIOptionsFor, etc. would reduce churn and improve readability.


      252-265: Release hygiene: this is a major public API surface—please add a changeset and deprecation plan. Given the new unified entrypoint + adapter split, shipping without a changeset will make downstream upgrades painful to track.

      docs/adapters/openai.md (1)

      59-79: Avoid hard-coding long model lists in docs (they’ll go stale fast). Prefer linking to a “supported models” reference generated from model-meta (or a short “examples” list) to reduce ongoing maintenance.

      packages/typescript/ai/src/activities/text/adapter.ts (1)

      23-28: Consider renaming chatOptions → textOptions (or similar) for consistency with “Text” terminology. This PR broadly renames “Chat” → “Text”; keeping “chatOptions” here is a small but pervasive inconsistency.

      packages/typescript/ai/src/activities/embedding/index.ts (1)

      68-70: Consider extracting createId to a shared utility.

      This helper function is duplicated from packages/typescript/ai/src/activities/text/index.ts (lines 951-953). To maintain DRY principles and ensure consistent ID generation across activities, consider extracting this to a shared utility module.

      -function createId(prefix: string): string {
      -  return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`
      -}
      +import { createId } from '../../utils/id.js'

      Then create packages/typescript/ai/src/utils/id.ts:

      export function createId(prefix: string): string {
        return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`
      }
      packages/typescript/ai/src/activities/index.ts (2)

      160-161: Consider stronger typing for ActivityHandler.

      The any types here lose type safety. While this flexibility is needed for the runtime map, consider documenting why or adding a comment explaining this trade-off.

      -/** Type for activity handler functions */
      -type ActivityHandler = (options: any) => any
      +/**
      + * Type for activity handler functions.
      + * Uses `any` intentionally as this is a runtime dispatch mechanism
      + * where type narrowing happens at the call site via adapter.kind.
      + */
      +type ActivityHandler = (options: unknown) => unknown

      490-515: Duplicate deprecated type aliases.

      These type aliases (lines 490-515) duplicate the functionality of the aliases at lines 301-327 with slightly different naming. Consider consolidating to one set to reduce maintenance overhead.

      Lines 301-327: GenerateTextOptions, GenerateEmbeddingOptions, etc. Lines 490-515: TextGenerateOptions, EmbeddingGenerateOptions, etc.

      Both point to the same underlying *ActivityOptions types.

      Either remove one set or have one reference the other:

       /** @deprecated Use TextActivityOptions */
       export type TextGenerateOptions<
         TAdapter extends TextAdapter<ReadonlyArray<string>, object, any, any, any>,
         TModel extends TextModels<TAdapter>,
         TSchema extends z.ZodType | undefined = undefined,
         TStream extends boolean = true,
      -> = TextActivityOptions<TAdapter, TModel, TSchema, TStream>
      +> = GenerateTextOptions<TAdapter, TModel, TSchema, TStream>
      packages/typescript/ai/src/activities/text/index.ts (3)

      1037-1068: Heavy type assertions may mask type errors.

      The multiple as unknown as casts bypass TypeScript's type checking. While these may be necessary due to complex generic constraints, they could mask type errors if the runtime values don't match expectations.

      Consider adding runtime assertions or restructuring the generics to reduce the need for double-casting:

      // Example: Add runtime assertion
      if (outputSchema) {
        if (typeof adapter.structuredOutput !== 'function') {
          throw new Error('Adapter does not support structured output')
        }
        return runAgenticStructuredOutput(options)
      }

      1155-1158: Stream consumption for side effects.

      The empty loop body consumes the stream purely for its side effects (executing the agentic loop). This is intentional and correct, but the comment could be more explicit about why this is necessary.

         // Consume the stream to run the agentic loop
         for await (const _chunk of engine.run()) {
      -    // Just consume the stream to execute the agentic loop
      +    // Intentionally discarding chunks - we only need the side effects
      +    // (tool execution, message accumulation) for structured output
         }

      1211-1212: Consider using TextAdapter instead of AIAdapter.

      The textOptions helper uses AIAdapter in its generic constraint while the rest of the text activity module uses TextAdapter. For consistency and to ensure only text-capable adapters are accepted, consider using TextAdapter.

       export function textOptions<
      -  TAdapter extends AIAdapter<any, any, any, any, any>,
      +  TAdapter extends TextAdapter<any, any, any, any, any>,
         const TModel extends TAdapter extends AIAdapter<

      This would also require updating the subsequent type references to use TextAdapter patterns.

      packages/typescript/ai/src/index.ts (3)

      1-16: Public API naming is getting redundant/confusing (GenerateOptions vs TextGenerateOptions vs GenerateTextOptions). Consider consolidating to one naming scheme (and/or adding /** @deprecated */ aliases) to reduce long-term API entropy.


      19-32: Avoid exporting from deep “activities/…” paths if these are meant to be stable public APIs. Re-exporting ./activities/text/tools/... bakes internal folder structure into the public surface; prefer a public barrel like ./tools (or ./text/tools) to keep refactors non-breaking.


      44-45: ToolCallManager being public: double-check this is intended “API surface”, not an implementation detail. If it’s internal plumbing, consider not exporting it from the package root (or clearly documenting it as advanced/unstable).

      📜 Review details

      Configuration used: CodeRabbit UI

      Review profile: CHILL

      Plan: Pro

      📥 Commits

      Reviewing files that changed from the base of the PR and between 935111d68e72f7364b93f78589bda2105832e020 and 96a08b1bbf6eac1e77b23958812a79c859461085.

      ⛔ Files ignored due to path filters (1)
      • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
      📒 Files selected for processing (107)
      • docs/adapters/anthropic.md (6 hunks)
      • docs/adapters/gemini.md (5 hunks)
      • docs/adapters/ollama.md (5 hunks)
      • docs/adapters/openai.md (9 hunks)
      • docs/api/ai.md (10 hunks)
      • docs/config.json (3 hunks)
      • docs/getting-started/overview.md (3 hunks)
      • docs/getting-started/quick-start.md (4 hunks)
      • docs/guides/agentic-cycle.md (2 hunks)
      • docs/guides/client-tools.md (3 hunks)
      • docs/guides/multimodal-content.md (6 hunks)
      • docs/guides/per-model-type-safety.md (2 hunks)
      • docs/guides/server-tools.md (2 hunks)
      • docs/guides/streaming.md (2 hunks)
      • docs/guides/tool-approval.md (1 hunks)
      • docs/guides/tool-architecture.md (1 hunks)
      • docs/guides/tools.md (4 hunks)
      • docs/protocol/http-stream-protocol.md (3 hunks)
      • docs/protocol/sse-protocol.md (2 hunks)
      • docs/reference/classes/BaseAdapter.md (1 hunks)
      • docs/reference/functions/text.md (1 hunks)
      • docs/reference/functions/textOptions.md (2 hunks)
      • docs/reference/index.md (3 hunks)
      • docs/reference/interfaces/AIAdapter.md (1 hunks)
      • docs/reference/interfaces/TextCompletionChunk.md (1 hunks)
      • docs/reference/interfaces/TextOptions.md (1 hunks)
      • docs/reference/type-aliases/TextStreamOptionsForModel.md (1 hunks)
      • docs/reference/type-aliases/TextStreamOptionsUnion.md (1 hunks)
      • examples/README.md (2 hunks)
      • examples/ts-group-chat/chat-server/capnweb-rpc.ts (7 hunks)
      • examples/ts-group-chat/chat-server/chat-logic.ts (2 hunks)
      • examples/ts-group-chat/chat-server/claude-service.ts (3 hunks)
      • examples/ts-group-chat/package.json (2 hunks)
      • examples/ts-react-chat/package.json (3 hunks)
      • examples/ts-react-chat/src/routes/api.tanchat.ts (4 hunks)
      • examples/ts-solid-chat/package.json (4 hunks)
      • examples/ts-solid-chat/src/routes/api.chat.ts (2 hunks)
      • examples/ts-svelte-chat/package.json (1 hunks)
      • examples/ts-svelte-chat/src/routes/api/chat/+server.ts (2 hunks)
      • examples/ts-vue-chat/package.json (1 hunks)
      • examples/ts-vue-chat/vite.config.ts (3 hunks)
      • examples/vanilla-chat/package.json (1 hunks)
      • package.json (1 hunks)
      • packages/typescript/ai-anthropic/package.json (1 hunks)
      • packages/typescript/ai-anthropic/src/adapters/text.ts (1 hunks)
      • packages/typescript/ai-anthropic/src/anthropic-adapter.ts (3 hunks)
      • packages/typescript/ai-anthropic/src/tools/custom-tool.ts (2 hunks)
      • packages/typescript/ai-anthropic/src/utils/index.ts (1 hunks)
      • packages/typescript/ai-anthropic/src/utils/schema-converter.ts (1 hunks)
      • packages/typescript/ai-anthropic/tests/anthropic-adapter.test.ts (3 hunks)
      • packages/typescript/ai-client/package.json (1 hunks)
      • packages/typescript/ai-devtools/package.json (1 hunks)
      • packages/typescript/ai-devtools/src/store/ai-context.tsx (3 hunks)
      • packages/typescript/ai-devtools/vite.config.ts (1 hunks)
      • packages/typescript/ai-gemini/package.json (1 hunks)
      • packages/typescript/ai-gemini/src/adapters/text.ts (1 hunks)
      • packages/typescript/ai-gemini/src/gemini-adapter.ts (3 hunks)
      • packages/typescript/ai-gemini/src/tools/tool-converter.ts (2 hunks)
      • packages/typescript/ai-gemini/src/utils/index.ts (1 hunks)
      • packages/typescript/ai-gemini/src/utils/schema-converter.ts (1 hunks)
      • packages/typescript/ai-gemini/tests/gemini-adapter.test.ts (6 hunks)
      • packages/typescript/ai-ollama/package.json (1 hunks)
      • packages/typescript/ai-ollama/src/adapters/text.ts (1 hunks)
      • packages/typescript/ai-ollama/src/ollama-adapter.ts (5 hunks)
      • packages/typescript/ai-ollama/src/utils/index.ts (1 hunks)
      • packages/typescript/ai-ollama/src/utils/schema-converter.ts (1 hunks)
      • packages/typescript/ai-openai/package.json (1 hunks)
      • packages/typescript/ai-openai/src/adapters/text.ts (1 hunks)
      • packages/typescript/ai-openai/src/openai-adapter.ts (5 hunks)
      • packages/typescript/ai-openai/src/tools/function-tool.ts (1 hunks)
      • packages/typescript/ai-openai/src/utils/index.ts (1 hunks)
      • packages/typescript/ai-openai/src/utils/schema-converter.ts (1 hunks)
      • packages/typescript/ai-openai/tests/openai-adapter.test.ts (2 hunks)
      • packages/typescript/ai-react-ui/package.json (1 hunks)
      • packages/typescript/ai-react/package.json (1 hunks)
      • packages/typescript/ai-solid-ui/package.json (1 hunks)
      • packages/typescript/ai-svelte/package.json (1 hunks)
      • packages/typescript/ai-vue-ui/package.json (1 hunks)
      • packages/typescript/ai/package.json (1 hunks)
      • packages/typescript/ai/src/activities/embedding/adapter.ts (1 hunks)
      • packages/typescript/ai/src/activities/embedding/index.ts (1 hunks)
      • packages/typescript/ai/src/activities/image/adapter.ts (1 hunks)
      • packages/typescript/ai/src/activities/image/index.ts (1 hunks)
      • packages/typescript/ai/src/activities/index.ts (1 hunks)
      • packages/typescript/ai/src/activities/summarize/adapter.ts (1 hunks)
      • packages/typescript/ai/src/activities/summarize/index.ts (1 hunks)
      • packages/typescript/ai/src/activities/text/adapter.ts (1 hunks)
      • packages/typescript/ai/src/activities/text/agent-loop-strategies.ts (1 hunks)
      • packages/typescript/ai/src/activities/text/index.ts (10 hunks)
      • packages/typescript/ai/src/activities/text/messages.ts (4 hunks)
      • packages/typescript/ai/src/activities/text/stream/message-updaters.ts (1 hunks)
      • packages/typescript/ai/src/activities/text/stream/processor.ts (2 hunks)
      • packages/typescript/ai/src/activities/text/stream/types.ts (1 hunks)
      • packages/typescript/ai/src/activities/text/tools/tool-calls.ts (1 hunks)
      • packages/typescript/ai/src/activities/text/tools/tool-definition.ts (1 hunks)
      • packages/typescript/ai/src/activities/text/tools/zod-converter.ts (1 hunks)
      • packages/typescript/ai/src/ai.ts (1 hunks)
      • packages/typescript/ai/src/base-adapter.ts (2 hunks)
      • packages/typescript/ai/src/core/chat-common-options.ts (0 hunks)
      • packages/typescript/ai/src/core/embedding.ts (0 hunks)
      • packages/typescript/ai/src/core/summarize.ts (0 hunks)
      • packages/typescript/ai/src/event-client.ts (4 hunks)
      • packages/typescript/ai/src/index.ts (4 hunks)
      • packages/typescript/ai/src/stream-to-response.ts (4 hunks)
      • packages/typescript/ai/src/tools/zod-converter.ts (0 hunks)
      • packages/typescript/ai/src/types.ts (11 hunks)
      • packages/typescript/ai/src/utilities/chat-options.ts (0 hunks)
      ⛔ Files not processed due to max files limit (36)
      • packages/typescript/ai/src/utilities/messages.ts
      • packages/typescript/ai/tests/agent-loop-strategies.test.ts
      • packages/typescript/ai/tests/ai-abort.test.ts
      • packages/typescript/ai/tests/ai-text.test.ts
      • packages/typescript/ai/tests/generate-types.test-d.ts
      • packages/typescript/ai/tests/generate.test.ts
      • packages/typescript/ai/tests/message-updaters.test.ts
      • packages/typescript/ai/tests/strategies.test.ts
      • packages/typescript/ai/tests/stream-processor-edge-cases.test.ts
      • packages/typescript/ai/tests/stream-processor-replay.test.ts
      • packages/typescript/ai/tests/stream-processor.test.ts
      • packages/typescript/ai/tests/stream-to-response.test.ts
      • packages/typescript/ai/tests/tool-call-manager.test.ts
      • packages/typescript/ai/tests/tool-definition.test.ts
      • packages/typescript/ai/tests/zod-converter.test.ts
      • packages/typescript/ai/vite.config.ts
      • packages/typescript/react-ai-devtools/package.json
      • packages/typescript/smoke-tests/adapters/.env.example
      • packages/typescript/smoke-tests/adapters/package.json
      • packages/typescript/smoke-tests/adapters/src/adapters/index.ts
      • packages/typescript/smoke-tests/adapters/src/cli.ts
      • packages/typescript/smoke-tests/adapters/src/harness.ts
      • packages/typescript/smoke-tests/adapters/src/index.ts
      • packages/typescript/smoke-tests/adapters/src/tests/ags-agentic-structured.ts
      • packages/typescript/smoke-tests/adapters/src/tests/apr-approval-flow.ts
      • packages/typescript/smoke-tests/adapters/src/tests/cst-chat-stream.ts
      • packages/typescript/smoke-tests/adapters/src/tests/emb-embedding.ts
      • packages/typescript/smoke-tests/adapters/src/tests/img-image-generation.ts
      • packages/typescript/smoke-tests/adapters/src/tests/index.ts
      • packages/typescript/smoke-tests/adapters/src/tests/ost-one-shot-text.ts
      • packages/typescript/smoke-tests/adapters/src/tests/str-structured-output.ts
      • packages/typescript/smoke-tests/adapters/src/tests/sum-summarize.ts
      • packages/typescript/smoke-tests/adapters/src/tests/tls-tool-server.ts
      • packages/typescript/smoke-tests/e2e/package.json
      • packages/typescript/smoke-tests/e2e/src/routes/api.tanchat.ts
      • packages/typescript/solid-ai-devtools/package.json
      💤 Files with no reviewable changes (5)
      • packages/typescript/ai/src/core/chat-common-options.ts
      • packages/typescript/ai/src/utilities/chat-options.ts
      • packages/typescript/ai/src/tools/zod-converter.ts
      • packages/typescript/ai/src/core/summarize.ts
      • packages/typescript/ai/src/core/embedding.ts
      ✅ Files skipped from review due to trivial changes (5)
      • packages/typescript/ai-react-ui/package.json
      • docs/reference/functions/text.md
      • packages/typescript/ai/src/activities/text/tools/tool-calls.ts
      • docs/reference/interfaces/TextCompletionChunk.md
      • examples/ts-vue-chat/package.json
      🚧 Files skipped from review as they are similar to previous changes (4)
      • packages/typescript/ai-anthropic/src/utils/index.ts
      • examples/ts-react-chat/src/routes/api.tanchat.ts
      • packages/typescript/ai-openai/src/utils/index.ts
      • packages/typescript/ai-gemini/src/utils/index.ts
      🧰 Additional context used
      🧬 Code graph analysis (19)
      packages/typescript/ai/src/activities/image/index.ts (1)
      packages/typescript/ai/src/types.ts (1)
      • ImageGenerationResult (786-799)
      packages/typescript/ai/src/base-adapter.ts (1)
      packages/typescript/ai/src/types.ts (2)
      • TextOptions (557-598)
      • StreamChunk (688-696)
      packages/typescript/ai-gemini/src/gemini-adapter.ts (1)
      packages/typescript/ai/src/types.ts (1)
      • TextOptions (557-598)
      examples/ts-group-chat/chat-server/chat-logic.ts (1)
      examples/ts-group-chat/src/hooks/useChatMessages.ts (1)
      • ChatMessage (4-10)
      packages/typescript/ai-openai/src/openai-adapter.ts (1)
      packages/typescript/ai-openai/src/text/text-provider-options.ts (1)
      • validateTextProviderOptions (320-325)
      packages/typescript/ai/src/activities/text/messages.ts (1)
      packages/typescript/ai/src/types.ts (2)
      • Modality (92-92)
      • ConstrainedModelMessage (300-316)
      packages/typescript/ai-devtools/src/store/ai-context.tsx (2)
      packages/typescript/ai/src/event-client.ts (1)
      • aiEventClient (309-309)
      packages/typescript/ai/src/index.ts (1)
      • aiEventClient (61-61)
      packages/typescript/ai-ollama/src/adapters/text.ts (2)
      packages/typescript/ai-ollama/src/utils/index.ts (4)
      • createOllamaClient (2-2)
      • convertZodToOllamaSchema (8-8)
      • generateId (4-4)
      • getOllamaHostFromEnv (5-5)
      packages/typescript/ai-ollama/src/utils/client.ts (3)
      • createOllamaClient (10-14)
      • generateId (39-41)
      • getOllamaHostFromEnv (20-34)
      packages/typescript/ai/src/activities/summarize/index.ts (2)
      packages/typescript/ai/src/types.ts (1)
      • StreamChunk (688-696)
      packages/typescript/ai/src/activities/text/index.ts (1)
      • createId (952-954)
      packages/typescript/ai-anthropic/src/anthropic-adapter.ts (1)
      packages/typescript/ai/src/types.ts (1)
      • TextOptions (557-598)
      packages/typescript/ai/src/types.ts (1)
      packages/typescript/ai/src/ai.ts (3)
      • TextAdapter (239-239)
      • EmbeddingAdapter (240-240)
      • SummarizeAdapter (241-241)
      packages/typescript/ai/src/activities/embedding/index.ts (1)
      packages/typescript/ai/src/activities/text/index.ts (1)
      • createId (952-954)
      packages/typescript/ai/src/activities/text/adapter.ts (1)
      packages/typescript/ai/src/types.ts (4)
      • TextOptions (557-598)
      • Modality (92-92)
      • DefaultMessageMetadataByModality (805-811)
      • StreamChunk (688-696)
      packages/typescript/ai-openai/tests/openai-adapter.test.ts (2)
      packages/typescript/ai/src/ai.ts (1)
      • ai (223-232)
      packages/typescript/ai/src/index.ts (1)
      • ai (3-3)
      packages/typescript/ai/src/activities/text/tools/zod-converter.ts (1)
      packages/typescript/ai/src/types.ts (1)
      • SchemaInput (62-62)
      packages/typescript/ai-anthropic/src/adapters/text.ts (4)
      packages/typescript/ai-anthropic/src/utils/index.ts (4)
      • AnthropicClientConfig (5-5)
      • createAnthropicClient (2-2)
      • generateId (3-3)
      • getAnthropicApiKeyFromEnv (4-4)
      packages/typescript/ai-anthropic/src/utils/client.ts (4)
      • AnthropicClientConfig (3-5)
      • createAnthropicClient (10-16)
      • generateId (43-45)
      • getAnthropicApiKeyFromEnv (22-38)
      packages/typescript/ai-anthropic/src/model-meta.ts (1)
      • ANTHROPIC_MODELS (363-373)
      packages/typescript/ai/src/types.ts (1)
      • ModelMessage (229-240)
      packages/typescript/ai-ollama/src/ollama-adapter.ts (1)
      packages/typescript/ai/src/types.ts (2)
      • TextOptions (557-598)
      • StreamChunk (688-696)
      packages/typescript/ai-openai/src/adapters/text.ts (3)
      packages/typescript/ai-openai/src/utils/index.ts (4)
      • OpenAIClientConfig (5-5)
      • createOpenAIClient (2-2)
      • generateId (4-4)
      • getOpenAIApiKeyFromEnv (3-3)
      packages/typescript/ai-openai/src/model-meta.ts (1)
      • OPENAI_CHAT_MODELS (1608-1653)
      packages/typescript/ai/src/types.ts (4)
      • TextOptions (557-598)
      • StreamChunk (688-696)
      • ModelMessage (229-240)
      • ContentPart (168-179)
      packages/typescript/ai/src/activities/index.ts (1)
      packages/typescript/ai/src/types.ts (4)
      • StreamChunk (688-696)
      • ImageGenerationResult (786-799)
      • ConstrainedModelMessage (300-316)
      • TextOptions (557-598)
      🪛 Biome (2.1.2)
      packages/typescript/ai-gemini/src/tools/tool-converter.ts

      [error] 80-82: Other switch clauses can erroneously access this declaration. Wrap the declaration in a block to restrict its access to the switch clause.

      The declaration is defined in this switch clause:

      Safe fix: Wrap the declaration in a block.

      (lint/correctness/noSwitchDeclarations)

      🪛 LanguageTool
      docs/adapters/ollama.md

      [grammar] ~126-~126: Use a hyphen to join words. Context: ...l, and qwen2` generally have good tool calling support. ## Provider Options O...

      (QB_NEW_EN_HYPHEN)

      docs/adapters/ollama.md line 117Resolvedby AlemTuzlak
      @@ -173,6 +173,6 @@
      173173 TanStack AI doesn't provide a built-in NDJSON formatter, but you can create one easily:
      174174
      175175 ```typescript
      176-import { chat } from '@tanstack/ai';
      177-import { openai } from '@tanstack/ai-openai';
      176+import { ai } from '@tanstack/ai';
      177+import { openaiText } from '@tanstack/ai-openai';
      178178
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟠 Major

      NDJSON error chunk shape is inconsistent with the document’s own examples.

      Earlier examples include id, model, timestamp on chunks, but the server snippets’ errorChunk only includes { type, error }. Either update the snippet to include the same envelope fields, or explicitly state that error chunks are allowed to be minimal.

      Suggested doc-snippet adjustment:

      -        const errorChunk = {
      -          type: 'error',
      -          error: {
      -            message: error.message || 'Unknown error',
      -            code: error.code,
      -          },
      -        };
      +        const errorChunk = {
      +          type: 'error',
      +          id: 'msg_1',
      +          model: 'gpt-4o',
      +          timestamp: Date.now(),
      +          error: {
      +            message: error?.message || 'Unknown error',
      +            code: error?.code,
      +          },
      +        };

      Also applies to: 183-187, 225-227, 239-243

      🤖 Prompt for AI Agents
      In docs/protocol/http-stream-protocol.md around lines 176-178 (and also apply
      same change to 183-187, 225-227, 239-243): the NDJSON error chunk examples are
      inconsistent with the earlier chunk envelope (which includes id, model,
      timestamp). Either change each server-side errorChunk snippet to include the
      same envelope fields (e.g., id, model, timestamp plus type and error) so the
      shape matches the document’s examples, or add a brief note above the snippets
      explicitly stating that error chunks may be minimal and do not include the
      envelope fields; make the chosen approach consistent across all listed ranges.
      @@ -1,16 +1,16 @@
      11 ---
      2-id: ChatStreamOptionsForModel
      3-title: ChatStreamOptionsForModel
      2+id: TextStreamOptionsForModel
      3+title: TextStreamOptionsForModel
      44 ---
      55
      6-# Type Alias: ChatStreamOptionsForModel\<TAdapter, TModel\>
      6+# Type Alias: TextStreamOptionsForModel\<TAdapter, TModel\>
      77
      88 ```ts
      9-type ChatStreamOptionsForModel<TAdapter, TModel> = TAdapter extends AIAdapter<any, any, any, any, infer ModelProviderOptions, infer ModelInputModalities, infer MessageMetadata> ? Omit<ChatOptions, "model" | "providerOptions" | "responseFormat" | "messages"> & object : never;
      9+type TextStreamOptionsForModel<TAdapter, TModel> = TAdapter extends AIAdapter<any, any, any, any, infer ModelProviderOptions, infer ModelInputModalities, infer MessageMetadata> ? Omit<TextOptions, "model" | "providerOptions" | "responseFormat" | "messages"> & object : never;
      1010 ```
      1111
      1212 Defined in: [types.ts:883](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/types.ts#L883)
      1313
      1414 Chat options constrained by a specific model's capabilities.
      15-Unlike ChatStreamOptionsUnion which creates a union over all models,
      15+Unlike TextStreamOptionsUnion which creates a union over all models,
      1616 this type takes a specific model and constrains messages accordingly.
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      Description still says “Chat options” after rename to TextStreamOptionsForModel.

      -Chat options constrained by a specific model's capabilities.
      +Text options constrained by a specific model's capabilities.
      📝 Committable suggestion

      ‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

      Suggested change
      2-id: TextStreamOptionsForModel
      3-title: TextStreamOptionsForModel
      4----
      5-
      6-# Type Alias: TextStreamOptionsForModel\<TAdapter, TModel\>
      7-
      8-```ts
      9-type TextStreamOptionsForModel<TAdapter, TModel> = TAdapter extends AIAdapter<any, any, any, any, infer ModelProviderOptions, infer ModelInputModalities, infer MessageMetadata> ? Omit<TextOptions, "model" | "providerOptions" | "responseFormat" | "messages"> & object : never;
      10-```
      11-
      12-Defined in: [types.ts:883](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/types.ts#L883)
      13-
      14-Chat options constrained by a specific model's capabilities.
      15-Unlike TextStreamOptionsUnion which creates a union over all models,
      16-this type takes a specific model and constrains messages accordingly.
      2+id: TextStreamOptionsForModel
      3+title: TextStreamOptionsForModel
      4+---
      5+
      6+# Type Alias: TextStreamOptionsForModel\<TAdapter, TModel\>
      7+
      🤖 Prompt for AI Agents
      docs/reference/type-aliases/TextStreamOptionsForModel.md around lines 2 to 16:
      the file header and paragraph still refer to "Chat options" after the type was
      renamed; update the descriptive text to say "Text stream options" (or "Text
      options") and adjust the comparison sentence to refer to TextStreamOptionsUnion
      (keep the same intent: this type constrains text stream/messages for a specific
      model rather than creating a union over all models). Ensure any leftover “chat”
      wording is replaced and the description matches the type name.
      @@ -44,9 +44,9 @@
      4444 "@tanstack/ai": "workspace:*"
      4545 },
      4646 "devDependencies": {
      47- "@vitest/coverage-v8": "4.0.14",
      48- "zod": "^4.1.13"
      47+ "@vitest/coverage-v8": "4.0.14"
      4948 },
      5049 "peerDependencies": {
      51- "@tanstack/ai": "workspace:*"
      50+ "@tanstack/ai": "workspace:*",
      51+ "zod": "^4.0.0"
      5252 }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      🧩 Analysis chain

      🏁 Script executed:

      fd -t f -e ts -e tsx -e js packages/typescript/ai-anthropic/src --type f

      Repository: TanStack/ai

      Length of output: 504


      🏁 Script executed:

      rg "import.*zod|from.*zod" packages/typescript/ai-anthropic --type ts --type tsx --type js -A 3

      Repository: TanStack/ai

      Length of output: 82


      🏁 Script executed:

      cat -n packages/typescript/ai-anthropic/package.json

      Repository: TanStack/ai

      Length of output: 1603


      🏁 Script executed:

      fd . 'packages/typescript/ai-anthropic/src' -t f

      Repository: TanStack/ai

      Length of output: 1212


      🏁 Script executed:

      rg "zod" packages/typescript/ai-anthropic/src -A 2 -B 2

      Repository: TanStack/ai

      Length of output: 2916


      🏁 Script executed:

      cat packages/typescript/ai-anthropic/package.json | grep -A 10 "peerDependenciesMeta"

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      cat packages/typescript/ai-anthropic/src/index.ts

      Repository: TanStack/ai

      Length of output: 1999


      🏁 Script executed:

      cat packages/typescript/ai-anthropic/src/utils/schema-converter.ts | head -60

      Repository: TanStack/ai

      Length of output: 1708


      🏁 Script executed:

      cat packages/typescript/ai-anthropic/src/tools/custom-tool.ts | head -50

      Repository: TanStack/ai

      Length of output: 1687


      🏁 Script executed:

      cat packages/typescript/ai-anthropic/src/tools/tool-converter.ts

      Repository: TanStack/ai

      Length of output: 2138


      🏁 Script executed:

      rg "convertCustomToolToAdapterFormat|inputSchema" packages/typescript/ai-anthropic/src --type ts -B 2 -A 2 | head -80

      Repository: TanStack/ai

      Length of output: 4579


      zod is a runtime dependency used conditionally for custom tools with input schemas.

      The package imports toJSONSchema from zod in schema-converter.ts and uses it at runtime when converting tool input schemas. This occurs in convertCustomToolToAdapterFormat(), which is called by the exported convertToolsToProviderFormat() function. While zod usage is conditional (triggered only when tool.inputSchema is provided), it is still a hard runtime requirement for that code path.

      The current placement as a required peerDependency is appropriate. However, if custom tools with schemas are not expected to be commonly used, consider adding peerDependenciesMeta: { zod: { optional: true } } with clear documentation that zod is required only for tools with input schemas. Otherwise, keep the current setup as-is.

      🤖 Prompt for AI Agents
      packages/typescript/ai-anthropic/package.json lines 46-52: the package currently
      lists "zod" as a required peerDependency but it is only needed at runtime when a
      consumer passes custom tools with input schemas; update package.json to mark the
      peer as optional by adding a peerDependenciesMeta entry for zod with optional:
      true (peerDependenciesMeta: { "zod": { "optional": true } }) and add a short
      note in the package README or docs stating that zod is required only when using
      tools with inputSchema so consumers know to install zod if they use that
      feature.
      @@ -0,0 +1,15 @@
      1+import { toJSONSchema } from 'zod'
      2+import type { z } from 'zod'
      3+
      4+/**
      5+ * Check if a value is a Zod schema by looking for Zod-specific internals.
      6+ * Zod schemas have a `_zod` property that contains metadata.
      7+ */
      8+function isZodSchema(schema: unknown): schema is z.ZodType {
      9+ return (
      10+ typeof schema === 'object' &&
      11+ schema !== null &&
      12+ '_zod' in schema &&
      13+ typeof (schema as any)._zod === 'object'
      14+ )
      15+}
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      🧩 Analysis chain

      🏁 Script executed:

      cat -n packages/typescript/ai-anthropic/src/utils/schema-converter.ts

      Repository: TanStack/ai

      Length of output: 2875


      🏁 Script executed:

      cat packages/typescript/ai-anthropic/package.json | grep -A 5 '"zod"'

      Repository: TanStack/ai

      Length of output: 80


      🏁 Script executed:

      cd packages/typescript/ai-anthropic && npm ls zod 2>/dev/null | head -20

      Repository: TanStack/ai

      Length of output: 148


      🌐 Web query:

      Zod v4 toJSONSchema export documentation API options target reused

      💡 Result:

      Key parts of Zod v4's z.toJSONSchema(schema, options) relevant to your query (defaults + short examples):

      • target — JSON‑Schema draft to emit. Default: "draft-2020-12". Accepts "draft-7", "draft-2020-12", "draft-4", "openapi-3.0", etc. Example: z.toJSONSchema(mySchema, { target: "draft-7" }). [1]

      • reused — how Zod handles the same schema repeated in a tree. Default behavior: inline repeated schemas. Set to "ref" to extract repeated schemas into $defs and emit $ref references. Example (inline, default): const name = z.string(); z.toJSONSchema(z.object({ firstName: name, lastName: name })); Example (refs): z.toJSONSchema(User, { reused: "ref" }); // produces $defs + $ref. [1]

      • unrepresentable — how to handle Zod types that have no JSON‑Schema equivalent (bigint, date, map, set, transform, etc.). Default: throw. Set to "any" to convert them to {} (JSON Schema unknown/any). Example: z.toJSONSchema(z.bigint(), { unrepresentable: "any" }). [1]

      • cycles — how to handle circular schemas. Default: represent cycles with $ref. Set to "throw" to error instead. Example: z.toJSONSchema(User, { cycles: "throw" }). [1]

      • override — callback to programmatically modify the default conversion. Receives ctx with ctx.zodSchema and ctx.jsonSchema; mutate ctx.jsonSchema in-place. Often used together with unrepresentable: "any". [1]

      Source: Zod v4 JSON‑Schema docs. [1]

      References [1] Zod — JSON Schema documentation (toJSONSchema options).


      🏁 Script executed:

      # Check if toJSONSchema is a known export from Zod 4.x
      cd packages/typescript/ai-anthropic && node -e "
      try {
        const zod = require('zod');
        console.log('toJSONSchema in zod:', 'toJSONSchema' in zod);
        console.log('Available exports:', Object.keys(zod).filter(k => k.includes('JSON')));
      } catch(e) {
        console.error('Error:', e.message);
      }
      " 2>/dev/null || echo "Node modules not available in sandbox, need to check docs"

      Repository: TanStack/ai

      Length of output: 37


      🌐 Web query:

      Zod package toJSONSchema function export zod@4

      💡 Result:

      Use Zod v4's built-in function z.toJSONSchema(schema). Example:

      import * as z from "zod/v4"; const schema = z.object({ name: z.string(), age: z.number() }); const json = z.toJSONSchema(schema);

      Docs: Zod JSON Schema page. [1] GitHub discussion/issues showing .toJSONSchema in v4. [2]


      Zod's toJSONSchema API and options are valid for Zod v4 — the function is exported from the 'zod' package, and both target: 'openapi-3.0' and reused: 'ref' are documented options. No breaking changes here.

      However, the type guard on line 59 is unsafe: typeof result === 'object' doesn't exclude null or arrays before assigning properties, which could cause runtime errors. Add null and Array checks:

      -  if (typeof result === 'object') {
      +  if (typeof result === 'object' && result !== null && !Array.isArray(result)) {

      The reliance on internal properties (_zod in line 12, schema.def.type in line 63) is a valid future-proofing concern and should be addressed by using only public Zod APIs or documenting the risk. The isZodSchema type guard on lines 8–14 can be simplified since the parameter is already typed as z.ZodType, making the runtime check redundant unless runtime safety is critical.

      Committable suggestion skipped: line range outside the PR's diff.

      🤖 Prompt for AI Agents
      In packages/typescript/ai-anthropic/src/utils/schema-converter.ts around lines
      1–15 (and referencing the later checks around ~59 and ~63), the current runtime
      type-guards are unsafe: the isZodSchema guard inspects an internal _zod property
      (and the later typeof result === 'object' check doesn't exclude null or arrays)
      which can lead to runtime crashes; update the guards to first ensure
      schema/result is not null and not an Array (use schema !== null &&
      !Array.isArray(schema)), or remove the runtime _zod check entirely since the
      parameter is already typed as z.ZodType, and replace any direct accesses to
      internal fields like schema.def.type with public Zod APIs or add a clear comment
      documenting the risk if internal access must remain; ensure all property reads
      are guarded after the null/array checks to prevent TypeError at runtime.
      @@ -76,5 +76,7 @@
      7676 )
      7777 }
      7878
      79- // Convert Zod schema to JSON Schema
      80- const jsonSchema = convertZodToJsonSchema(tool.inputSchema)
      79+ // Convert Zod schema to Gemini-compatible JSON Schema
      80+ const jsonSchema = tool.inputSchema
      81+ ? convertZodToGeminiSchema(tool.inputSchema)
      82+ : { type: 'object', properties: {}, required: [] }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      Wrap declaration in a block to prevent variable leakage.

      The jsonSchema variable declaration in the switch clause can be erroneously accessed by other cases. This is a correctness issue that could lead to bugs.

      Apply this diff to wrap the declaration in a block:

             default:
      +        {
               // Collect function declarations to group together
               // Description is required for Gemini function declarations
               if (!tool.description) {
                 throw new Error(
                   `Tool ${tool.name} requires a description for Gemini adapter`,
                 )
               }
       
               // Convert Zod schema to Gemini-compatible JSON Schema
               const jsonSchema = tool.inputSchema
                 ? convertZodToGeminiSchema(tool.inputSchema)
                 : { type: 'object', properties: {}, required: [] }
       
               functionDeclarations.push({
                 name: tool.name,
                 description: tool.description,
                 parameters: jsonSchema,
               })
               break
      +        }

      Committable suggestion skipped: line range outside the PR's diff.

      🧰 Tools
      🪛 Biome (2.1.2)

      [error] 80-82: Other switch clauses can erroneously access this declaration. Wrap the declaration in a block to restrict its access to the switch clause.

      The declaration is defined in this switch clause:

      Safe fix: Wrap the declaration in a block.

      (lint/correctness/noSwitchDeclarations)

      🤖 Prompt for AI Agents
      In packages/typescript/ai-gemini/src/tools/tool-converter.ts around lines 79 to
      82, the jsonSchema variable is declared inside a switch case but not wrapped in
      its own block, which allows it to leak into other cases; to fix, wrap the case
      body in braces { ... } and move the const jsonSchema declaration inside those
      braces so it has block scope, ensuring each case defines its own jsonSchema and
      preventing accidental access from other cases.
      @@ -0,0 +1,15 @@
      1+import { toJSONSchema } from 'zod'
      2+import type { z } from 'zod'
      3+
      4+/**
      5+ * Check if a value is a Zod schema by looking for Zod-specific internals.
      6+ * Zod schemas have a `_zod` property that contains metadata.
      7+ */
      8+function isZodSchema(schema: unknown): schema is z.ZodType {
      9+ return (
      10+ typeof schema === 'object' &&
      11+ schema !== null &&
      12+ '_zod' in schema &&
      13+ typeof (schema as any)._zod === 'object'
      14+ )
      15+}
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟠 Major

      🧩 Analysis chain

      🌐 Web query:

      Zod schema type checking public API instanceof ZodType

      💡 Result:

      Short answer

      • At runtime you can check specific schema classes with instanceof (e.g. schema instanceof z.ZodObject, z.ZodString).
      • To test “is this a Zod schema” in a robust, cross-bundle way prefer duck-typing (check for _def and safeParse/parse) because instanceof can fail if multiple Zod copies/versions are loaded.

      Example — instanceof (works when same Zod runtime):

      • if (schema instanceof z.ZodObject) { / object schema / }

      Example — duck-typing (recommended for libraries):

      • function isZodSchema(x: unknown): x is z.ZodTypeAny { return !!x && typeof (x as any)._def === 'object' && typeof (x as any).safeParse === 'function'; }

      TypeScript typing for generics:

      • For generic helpers accept T extends z.ZodTypeAny (not plain ZodType) so you preserve concrete schema methods and types.

      Caveats

      • instanceof relies on the schema being an instance of the exact Zod class from the same package copy — it may fail across different Zod major versions or duplicated installs; duck-typing avoids that.

      Sources: [Zod docs/examples showing instanceof], [Zod advice about instanceof failing across copies/versions], [Zod recommendation to use ZodTypeAny for generic helpers].

      References [1] Zod utilities/docs (instanceof examples). [2] Zod versioning / instance-of note (instanceof may fail across copies). [3] Zod npm docs (use ZodTypeAny for generic functions).


      🌐 Web query:

      Zod library _zod property internal or public documentation

      💡 Result:

      Short answer: _zod is an internal internals property (not a supported public API). The Zod core packages expose an internal _zod object that contains the schema internals (e.g., .def) intended for tooling/extension, but it is not part of the public, stable API surface you should rely on for production. See the Zod core docs explaining the internals and the main Zod site. [1][2]

      Sources

      • Zod core internals docs (explains the _zod internals and .def). [1]
      • Zod official docs / package overview. [2]

      🏁 Script executed:

      # Search for how _zod is used or documented in the codebase
      rg "_zod" packages/typescript/ai-gemini/ -A 2 -B 2

      Repository: TanStack/ai

      Length of output: 1055


      🏁 Script executed:

      # Check what version of Zod is used in this project
      fd package.json -x grep -A 20 "zod" {} +

      Repository: TanStack/ai

      Length of output: 11788


      Use duck-typing pattern to check Zod schemas instead of internal _zod property.

      The isZodSchema function checks for the internal _zod property, which is not part of Zod's public API and may change. Replace with the recommended duck-typing approach that checks for _def and safeParse:

      function isZodSchema(schema: unknown): schema is z.ZodTypeAny {
        return (
          !!schema &&
          typeof (schema as any)._def === 'object' &&
          typeof (schema as any).safeParse === 'function'
        )
      }

      This pattern is more robust across Zod versions and multiple bundle scenarios.

      🤖 Prompt for AI Agents
      In packages/typescript/ai-gemini/src/utils/schema-converter.ts around lines 8 to
      15, the current isZodSchema uses the internal `_zod` property which is unstable;
      replace it with duck-typing that verifies presence of `_def` and `safeParse`.
      Update the type guard to return schema is z.ZodTypeAny, check that schema is
      truthy, that (schema as any)._def is an object, and that (schema as
      any).safeParse is a function, so the guard works across Zod versions and
      bundles.
      @@ -42,13 +42,13 @@
      4242 ],
      4343 "dependencies": {
      4444 "@tanstack/ai": "workspace:*",
      45- "ollama": "^0.6.3",
      46- "zod": "^4.1.13"
      45+ "ollama": "^0.6.3"
      4746 },
      4847 "devDependencies": {
      4948 "@vitest/coverage-v8": "4.0.14",
      50- "vite": "^7.2.4"
      49+ "vite": "^7.2.7"
      5150 },
      5251 "peerDependencies": {
      53- "@tanstack/ai": "workspace:*"
      52+ "@tanstack/ai": "workspace:*",
      53+ "zod": "^4.0.0"
      5454 }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟠 Major

      🧩 Analysis chain

      🏁 Script executed:

      cat -n packages/typescript/ai-ollama/package.json

      Repository: TanStack/ai

      Length of output: 1640


      🏁 Script executed:

      find packages/typescript/ai-ollama/src -type f \( -name "*.ts" -o -name "*.tsx" -o -name "*.js" \) -exec grep -l "zod" {} \;

      Repository: TanStack/ai

      Length of output: 114


      🏁 Script executed:

      rg "from ['\"](zod|@tanstack/ai)" packages/typescript/ai-ollama/src --no-heading

      Repository: TanStack/ai

      Length of output: 1290


      Add zod as a devDependency (currently missing but imported in src/utils/schema-converter.ts).

      The package imports zod for toJSONSchema and type utilities in the source, so it must be available for local builds, typechecking, and tests. Peer-only dependencies can break development workflows.

        "devDependencies": {
          "@vitest/coverage-v8": "4.0.14",
      +   "zod": "^4.0.0",
          "vite": "^7.2.7"
        },
        "peerDependencies": {
          "@tanstack/ai": "workspace:*",
          "zod": "^4.0.0"
        }

      Also, @tanstack/ai is listed in both dependencies and peerDependencies—verify this dual presence is intentional for your packaging design.

      📝 Committable suggestion

      ‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

      Suggested change
      47- "devDependencies": {
      48- "@vitest/coverage-v8": "4.0.14",
      49- "vite": "^7.2.7"
      50- },
      51- "peerDependencies": {
      52- "@tanstack/ai": "workspace:*",
      53- "zod": "^4.0.0"
      54- }
      47+ "devDependencies": {
      48+ "@vitest/coverage-v8": "4.0.14",
      49+ "zod": "^4.0.0",
      50+ "vite": "^7.2.7"
      51+ },
      52+ "peerDependencies": {
      53+ "@tanstack/ai": "workspace:*",
      54+ "zod": "^4.0.0"
      55+ }
      🤖 Prompt for AI Agents
      In packages/typescript/ai-ollama/package.json around lines 47 to 54, the package
      imports zod in src/utils/schema-converter.ts but zod is only listed as a
      peerDependency; add zod to devDependencies (e.g., "zod": "^4.0.0") so local
      builds/typechecking/tests can resolve it, and then verify @tanstack/ai appearing
      in both dependencies and peerDependencies is intentional—if not, remove it from
      dependencies (or from peerDependencies) so only the intended entry remains and
      versions align with workspace:*.
      @@ -0,0 +77,21 @@
      77+
      78+ async *chatStream(
      79+ options: TextOptions<string, OpenAITextProviderOptions>,
      80+ ): AsyncIterable<StreamChunk> {
      81+ // Track tool call metadata by unique ID
      82+ // OpenAI streams tool calls with deltas - first chunk has ID/name, subsequent chunks only have args
      83+ // We assign our own indices as we encounter unique tool call IDs
      84+ const toolCallMetadata = new Map<string, { index: number; name: string }>()
      85+ const requestArguments = this.mapTextOptionsToOpenAI(options)
      86+
      87+ try {
      88+ const response = await this.client.responses.create(
      89+ {
      90+ ...requestArguments,
      91+ stream: true,
      92+ },
      93+ {
      94+ headers: options.request?.headers,
      95+ signal: options.request?.signal,
      96+ },
      97+ )
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟠 Major

      🧩 Analysis chain

      🏁 Script executed:

      find . -name "text.ts" -path "*/ai-openai/*" | head -20

      Repository: TanStack/ai

      Length of output: 107


      🏁 Script executed:

      head -200 packages/typescript/ai-openai/src/adapters/text.ts | tail -120

      Repository: TanStack/ai

      Length of output: 3955


      🏁 Script executed:

      rg "interface TextOptions|type TextOptions" packages/typescript/ai-openai/src/ -A 20

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      rg "interface StructuredOutputOptions|type StructuredOutputOptions" packages/typescript/ai-openai/src/ -A 15

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      rg "abortController" packages/typescript/ai-openai/src/adapters/text.ts -B 2 -A 2

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      head -100 packages/typescript/ai-openai/src/adapters/text.ts

      Repository: TanStack/ai

      Length of output: 3109


      🏁 Script executed:

      rg "class.*Text|export" packages/typescript/ai-openai/src/adapters/text.ts | head -20

      Repository: TanStack/ai

      Length of output: 304


      🏁 Script executed:

      find packages/typescript/ai-openai/src -name "*.ts" -type f | grep -E "(type|index)" | head -20

      Repository: TanStack/ai

      Length of output: 1249


      🏁 Script executed:

      find packages -name "*.ts" -type f | xargs grep -l "interface TextOptions\|export type TextOptions" | head -10

      Repository: TanStack/ai

      Length of output: 90


      🏁 Script executed:

      rg "interface TextOptions|export type TextOptions" packages/ -A 15 | head -80

      Repository: TanStack/ai

      Length of output: 2168


      🏁 Script executed:

      cat packages/typescript/ai-openai/src/text/text-provider-options.ts

      Repository: TanStack/ai

      Length of output: 16154


      🏁 Script executed:

      find . -path "*/node_modules/openai/*" -name "*.d.ts" | head -20

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      rg "responses\.create|RequestOptions" packages/typescript/ai-openai/src -B 3 -A 3

      Repository: TanStack/ai

      Length of output: 1738


      🏁 Script executed:

      rg "abortController|abort" packages/typescript/ai -B 2 -A 2 | head -100

      Repository: TanStack/ai

      Length of output: 7228


      🏁 Script executed:

      rg "interface CommonOptions|export type CommonOptions" packages/typescript/ai/src -A 30 | head -60

      Repository: TanStack/ai

      Length of output: 2837


      🏁 Script executed:

      rg "getAbortSignal" packages/typescript/ai/src -B 3 -A 10 | head -80

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      rg "class BaseAdapter|class BaseTextAdapter" packages/typescript/ai/src -A 50 | head -100

      Repository: TanStack/ai

      Length of output: 8439


      🏁 Script executed:

      rg "getAbortSignal" packages/typescript/ -B 2 -A 15

      Repository: TanStack/ai

      Length of output: 1917


      🏁 Script executed:

      grep -n "abortController" packages/typescript/ai/src/types.ts -B 10 -A 5

      Repository: TanStack/ai

      Length of output: 851


      🏁 Script executed:

      web_search
      OpenAI Node SDK client.responses.create headers parameter type - does it accept Headers object or require plain record

      Repository: TanStack/ai

      Length of output: 150


      🌐 Web query:

      OpenAI Node SDK client.responses.create headers parameter - does it accept Headers object or plain record

      💡 Result:

      Short answer: The Node SDK expects a plain Record<string,string> for request "headers" — passing a web Headers instance can cause TypeScript type errors and has been reported as a bug. See the SDK issue showing the Headers vs Record<string,string> mismatch [1] and the library docs/examples which use plain header records (and expose raw Response.headers separately) [2].

      Workarounds: convert Headers -> plain object before passing (Object.fromEntries(headers.entries())) or cast to any/Record<string,string>.

      Sources: [1] GitHub issue: "Headers Type Mismatch" (openai/openai-node #1405). [2] openai-node README / docs (Responses API / access raw Response headers).


      🏁 Script executed:

      rg "headers|Headers|fromEntries" packages/typescript/ai-openai/src -B 2 -A 2 | head -60

      Repository: TanStack/ai

      Length of output: 2011


      🏁 Script executed:

      rg "abortController?.signal|abortController" packages/typescript/ai-openai/src/adapters/text.ts -B 3 -A 3

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      rg "class OpenAITextAdapter|constructor" packages/typescript/ai-openai/src/adapters/text.ts -A 20 | head -40

      Repository: TanStack/ai

      Length of output: 1430


      🏁 Script executed:

      cat packages/typescript/ai/src/types.ts | grep -A 30 "export interface TextOptions"

      Repository: TanStack/ai

      Length of output: 1250


      Normalize headers to plain object before passing to OpenAI SDK, and use abortController.signal instead of options.request?.signal.

      TextOptions.request can be Request or RequestInit where headers may be a Headers object, but the OpenAI Node SDK expects headers to be a plain Record<string, string>. Passing a Headers object directly causes type errors and runtime failures. Convert using Object.fromEntries(headers.entries()) when headers are present.

      Additionally, TextOptions includes abortController for request cancellation, but it's being ignored—only options.request?.signal is passed. Use options.abortController?.signal ?? options.request?.signal to respect both sources, prioritizing the explicit abortController.

      Also applies to: lines 137-155

      🤖 Prompt for AI Agents
      In packages/typescript/ai-openai/src/adapters/text.ts around lines 88-97 (and
      also update the similar block at lines 137-155), the code currently passes
      options.request?.headers and options.request?.signal directly to the OpenAI SDK;
      convert any Headers object to a plain Record<string,string> before passing
      (e.g., when headers exists use Object.fromEntries(headers.entries())) so the SDK
      gets a plain object, and use options.abortController?.signal ??
      options.request?.signal for the signal parameter so an explicit abortController
      is honored; ensure both places perform the same normalization and signal
      selection.
      @@ -0,0 +93,21 @@
      93+ {
      94+ headers: options.request?.headers,
      95+ signal: options.request?.signal,
      96+ },
      97+ )
      98+
      99+ // Chat Completions API uses SSE format - iterate directly
      100+ yield* this.processOpenAIStreamChunks(
      101+ response,
      102+ toolCallMetadata,
      103+ options,
      104+ () => generateId(this.name),
      105+ )
      106+ } catch (error: unknown) {
      107+ const err = error as Error
      108+ console.error('>>> chatStream: Fatal error during response creation <<<')
      109+ console.error('>>> Error message:', err.message)
      110+ console.error('>>> Error stack:', err.stack)
      111+ console.error('>>> Full error:', err)
      112+ throw error
      113+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟠 Major

      chatStream() should yield an error chunk (like other adapters) instead of throwing from the generator. Throwing forces consumers to wrap the iteration itself in try/catch and prevents unified stream error handling.

      -    } catch (error: unknown) {
      -      const err = error as Error
      -      console.error('>>> chatStream: Fatal error during response creation <<<')
      -      console.error('>>> Error message:', err.message)
      -      console.error('>>> Error stack:', err.stack)
      -      console.error('>>> Full error:', err)
      -      throw error
      -    }
      +    } catch (error: unknown) {
      +      const err = error as Error
      +      const timestamp = Date.now()
      +      yield {
      +        type: 'error',
      +        id: generateId(this.name),
      +        model: options.model,
      +        timestamp,
      +        error: { message: err?.message ?? 'Unknown error occurred' },
      +      }
      +      return
      +    }

      Committable suggestion skipped: line range outside the PR's diff.

      🤖 Prompt for AI Agents
      In packages/typescript/ai-openai/src/adapters/text.ts around lines 106 to 113,
      the catch block currently throws the error which forces callers to wrap
      iteration in try/catch; instead yield an error chunk consistent with other
      adapters (e.g., { type: 'error', error: { message: err.message, stack:
      err.stack, ... } }) so stream consumers can handle errors uniformly, then
      return/close the generator (do not re-throw). Ensure the yielded payload shape
      matches the other adapters' error chunk format and remove the final throw.
      @@ -0,0 +663,21 @@
      663+ // For base64 data, construct a data URI
      664+ return {
      665+ type: 'input_image',
      666+ image_url: part.source.value,
      667+ detail: imageMetadata?.detail || 'auto',
      668+ }
      669+ }
      670+ case 'audio': {
      671+ if (part.source.type === 'url') {
      672+ // OpenAI may support audio URLs in the future
      673+ // For now, treat as data URI
      674+ return {
      675+ type: 'input_file',
      676+ file_url: part.source.value,
      677+ }
      678+ }
      679+ return {
      680+ type: 'input_file',
      681+ file_data: part.source.value,
      682+ }
      683+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🔴 Critical

      🧩 Analysis chain

      🌐 Web query:

      OpenAI Responses API input schema file_url file_data input_file audio

      💡 Result:

      Summary — how to attach files/audio to OpenAI Responses API inputs

      • input items use type "input_file" (in the input[].content array). You can reference:

        • file_id — ID of a file previously uploaded to /v1/files. [1]
        • file_url — an external HTTPS URL the API will fetch (supported for PDFs). [1]
        • file_data — base64/data-URI inline file content (clients/libraries may require also providing filename). Use this when you want to send the file inline instead of uploading separately. [2][3]
      • Audio: the Responses API accepts audio via the API’s audio endpoints (speech / transcription) or as file inputs where supported, but note some client libraries disallow sending raw binary audio as inline file_data for Responses; libraries may require uploading audio to /v1/files first and then referencing the file_id. The community / client code shows audio-as-binary inline is often not implemented/supported. [1][2][3]

      Examples:

      • Reference an uploaded file_id in a response request: { "type":"input_file", "file_id":"file-XXXX" }. [1]
      • Reference an external PDF URL: { "type":"input_file", "file_url":"https://example.com/doc.pdf" }. [1]
      • Inline (base64) file (client may need filename field): { "type":"input_file", "file_data":"data:application/pdf;base64,....", "filename":"doc.pdf" }. [2][3]

      Sources: [1] OpenAI — File inputs (Responses API) (platform docs). [2] OpenAI Responses API examples / File input usage (community & examples). [3] Pydantic/client implementation notes showing file_data and filename usage and audio-binary limitations.


      🌐 Web query:

      OpenAI Responses API audio input input_audio file handling

      💡 Result:

      Short answer: The Responses API does not accept audio file uploads. Use the Audio (transcriptions/translations) or Realtime endpoints instead — send files as multipart/form-data (e.g., -F file="@/path/to/file" plus model), obey supported formats and size limits (25 MB for the Audio API), and follow the Audio/Realtime docs for transcription or speech-capable models. Sources: OpenAI Audio guide [1], Audio transcription endpoint docs (multipart/form-data example) [2], Audio API FAQ (file-size/format limits) [3].


      🏁 Script executed:

      cat -n packages/typescript/ai-openai/src/adapters/text.ts | head -100

      Repository: TanStack/ai

      Length of output: 3809


      🏁 Script executed:

      sed -n '650,700p' packages/typescript/ai-openai/src/adapters/text.ts

      Repository: TanStack/ai

      Length of output: 1423


      🏁 Script executed:

      sed -n '600,650p' packages/typescript/ai-openai/src/adapters/text.ts

      Repository: TanStack/ai

      Length of output: 1263


      Remove audio handling from Responses API adapter—the OpenAI Responses API does not support audio inputs at all.

      The code attempts to send audio via input_file type in the Responses API, but the Responses API only accepts file inputs for documents (PDFs, etc.), not audio. Audio must use the dedicated Audio API endpoints (transcriptions/translations/realtime) instead. This code will fail at runtime whenever an audio ContentPart is passed. Either remove the audio case from this text adapter or throw an explicit error indicating audio is unsupported in the Responses API, with guidance to use the appropriate Audio API endpoints instead.

      🤖 Prompt for AI Agents
      packages/typescript/ai-openai/src/adapters/text.ts lines 670-683: the Responses
      API adapter currently handles 'audio' ContentPart by returning an input_file
      payload which is unsupported and will fail at runtime; remove the 'audio' case
      or replace it with an explicit thrown error explaining that audio is not
      supported by the Responses API and direct callers to use the Audio API
      (transcriptions/translations/realtime) instead so that any audio ContentPart is
      rejected early with a clear message.
      @@ -0,0 +136,21 @@
      136+ })
      137+
      138+ const embeddingOptions: EmbeddingOptions = {
      139+ model: model as string,
      140+ input,
      141+ dimensions,
      142+ }
      143+
      144+ const result = await adapter.createEmbeddings(embeddingOptions)
      145+
      146+ const duration = Date.now() - startTime
      147+
      148+ aiEventClient.emit('embedding:completed', {
      149+ requestId,
      150+ model: model as string,
      151+ inputCount,
      152+ duration,
      153+ timestamp: Date.now(),
      154+ })
      155+
      156+ return result
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      Missing error handling may leave unpaired lifecycle events.

      If adapter.createEmbeddings throws an exception, embedding:started will have been emitted but embedding:completed will never be emitted. This could cause issues with monitoring/tracing systems expecting paired events.

      Consider wrapping in try-catch to ensure consistent event pairing:

         aiEventClient.emit('embedding:started', {
           requestId,
           model: model as string,
           inputCount,
           timestamp: startTime,
         })
      
      -  const embeddingOptions: EmbeddingOptions = {
      -    model: model as string,
      -    input,
      -    dimensions,
      -  }
      -
      -  const result = await adapter.createEmbeddings(embeddingOptions)
      -
      -  const duration = Date.now() - startTime
      -
      -  aiEventClient.emit('embedding:completed', {
      -    requestId,
      -    model: model as string,
      -    inputCount,
      -    duration,
      -    timestamp: Date.now(),
      -  })
      -
      -  return result
      +  const embeddingOptions: EmbeddingOptions = {
      +    model: model as string,
      +    input,
      +    dimensions,
      +  }
      +
      +  try {
      +    const result = await adapter.createEmbeddings(embeddingOptions)
      +
      +    const duration = Date.now() - startTime
      +
      +    aiEventClient.emit('embedding:completed', {
      +      requestId,
      +      model: model as string,
      +      inputCount,
      +      duration,
      +      timestamp: Date.now(),
      +    })
      +
      +    return result
      +  } catch (error) {
      +    aiEventClient.emit('embedding:failed', {
      +      requestId,
      +      model: model as string,
      +      inputCount,
      +      duration: Date.now() - startTime,
      +      error: error instanceof Error ? error.message : 'Unknown error',
      +      timestamp: Date.now(),
      +    })
      +    throw error
      +  }
      🤖 Prompt for AI Agents
      In packages/typescript/ai/src/activities/embedding/index.ts around lines 131 to
      156, the call to adapter.createEmbeddings can throw and leaves the prior
      'embedding:started' event unpaired; wrap the createEmbeddings call in a
      try-catch-finally: in try await the adapter and capture result; in catch emit a
      failure event (or emit 'embedding:completed' with an error flag and error
      message) including requestId, model, inputCount, duration and timestamp, then
      rethrow the error; ensure duration is computed in finally so the emitted
      completion/failure always includes elapsed time and the started/completed
      lifecycle is paired.
      @@ -0,0 +1 @@
      1+import { toJSONSchema } from 'zod'
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      🧩 Analysis chain

      🏁 Script executed:

      # Find and inspect package.json files to check zod version
      find . -name "package.json" -type f | head -20

      Repository: TanStack/ai

      Length of output: 924


      🏁 Script executed:

      # Check the specific file's line 181 to see the full comment
      sed -n '175,190p' packages/typescript/ai/src/activities/text/tools/zod-converter.ts

      Repository: TanStack/ai

      Length of output: 477


      🏁 Script executed:

      # Check package.json in the project root and relevant workspace directories
      if [ -f "package.json" ]; then
        echo "=== Root package.json zod dependency ===" 
        jq '.dependencies.zod // .devDependencies.zod // "not found"' package.json
      fi
      
      # Check in the typescript/ai package
      if [ -f "packages/typescript/ai/package.json" ]; then
        echo "=== packages/typescript/ai/package.json zod dependency ==="
        jq '.dependencies.zod // .devDependencies.zod // "not found"' packages/typescript/ai/package.json
      fi

      Repository: TanStack/ai

      Length of output: 176


      🌐 Web query:

      zod v4 toJSONSchema function API documentation

      💡 Result:

      z.toJSONSchema(schema, params?) — converts a Zod v4 schema to JSON Schema. Key details:

      • Signature: z.toJSONSchema(schema, ToJSONSchemaParams?). (zod.dev)
      • Important ToJSONSchemaParams:
        • target?: "draft-4" | "draft-7" | "draft-2020-12" | "openapi-3.0" (default: draft-2020-12). (zod.dev)
        • metadata?: $ZodRegistry<Record<string, any>> (use z.globalRegistry / .meta()). (zod.dev)
        • unrepresentable?: "throw" | "any" (default "throw") — controls handling of Zod types that have no JSON Schema equivalent. (zod.dev)
        • cycles?: "ref" | "throw" (default "ref") — how to handle cyclic schemas. (zod.dev)
        • reused?: "ref" | "inline" (default "inline") — how to handle reused schemas. (zod.dev)
        • uri?: (id: string) => string — mapper for external $ref URIs. (zod.dev)
      • Notes:
        • Some Zod types are unrepresentable (bigint, date, map, set, transform, etc.); default behavior is to throw unless unrepresentable: "any" is set. (zod.dev)
        • Converts many Zod checks to formats, patterns, numeric constraints, object additionalProperties behavior, etc.; see the JSON Schema docs for detailed mapping and examples. (zod.dev)

      Official docs (full reference and examples): Zod JSON Schema docs. (zod.dev)

      Citations:


      🏁 Script executed:

      # Check if zod package is installed and examine its exports
      if [ -d "node_modules/zod" ]; then
        echo "=== Checking zod package exports ==="
        cat node_modules/zod/package.json | jq '.exports' | head -30
        
        echo -e "\n=== Checking zod lib/types.d.ts for toJSONSchema ==="
        grep -n "toJSONSchema" node_modules/zod/lib/types.d.ts | head -5
      fi

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      # Search the codebase for any other references to Alcyone Labs or toJSONSchema to understand usage pattern
      rg -i "alcyone|toJSONSchema" --type ts --type js -B 2 -A 2

      Repository: TanStack/ai

      Length of output: 22211


      Update comment on line 181 to remove outdated fork reference.

      The import import { toJSONSchema } from 'zod' is correct. However, the comment "Use Alcyone Labs fork which is compatible with Zod v4" is outdated. toJSONSchema is now part of Zod v4's standard API. Update the comment to reflect this: // Use Zod's built-in toJSONSchema for OpenAI 3.0 compatibility or similar.

      🤖 Prompt for AI Agents
      In packages/typescript/ai/src/activities/text/tools/zod-converter.ts around line
      181, update the outdated comment that references the Alcyone Labs fork; replace
      it with a current comment noting that toJSONSchema is provided by Zod v4 (e.g.
      "// Use Zod's built-in toJSONSchema for OpenAI 3.0 compatibility") so the
      comment accurately reflects that toJSONSchema is part of Zod v4's standard API.
      @@ -0,0 +52,21 @@
      52+ properties[propName] = {
      53+ ...prop,
      54+ items: makeStructuredOutputCompatible(
      55+ prop.items,
      56+ prop.items.required || [],
      57+ ),
      58+ }
      59+ } else if (wasOptional) {
      60+ // Make optional fields nullable by adding null to the type
      61+ if (prop.type && !Array.isArray(prop.type)) {
      62+ properties[propName] = {
      63+ ...prop,
      64+ type: [prop.type, 'null'],
      65+ }
      66+ } else if (Array.isArray(prop.type) && !prop.type.includes('null')) {
      67+ properties[propName] = {
      68+ ...prop,
      69+ type: [...prop.type, 'null'],
      70+ }
      71+ }
      72+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      Handle edge case: optional fields without type property.

      The logic for making optional fields nullable assumes prop.type exists. However, JSON Schema properties can omit type when using oneOf, anyOf, or other combinators. If an optional field lacks a type property, it won't be made nullable, which could violate OpenAI's structured output requirements.

      Consider adding a fallback for properties without type:

             } else if (wasOptional) {
               // Make optional fields nullable by adding null to the type
               if (prop.type && !Array.isArray(prop.type)) {
                 properties[propName] = {
                   ...prop,
                   type: [prop.type, 'null'],
                 }
               } else if (Array.isArray(prop.type) && !prop.type.includes('null')) {
                 properties[propName] = {
                   ...prop,
                   type: [...prop.type, 'null'],
                 }
      +        } else if (!prop.type && (prop.oneOf || prop.anyOf || prop.allOf)) {
      +          // For schema composition, wrap in anyOf with null
      +          properties[propName] = {
      +            ...prop,
      +            anyOf: [...(prop.anyOf || [prop]), { type: 'null' }],
      +          }
               }
             }
      📝 Committable suggestion

      ‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

      Suggested change
      59- } else if (wasOptional) {
      60- // Make optional fields nullable by adding null to the type
      61- if (prop.type && !Array.isArray(prop.type)) {
      62- properties[propName] = {
      63- ...prop,
      64- type: [prop.type, 'null'],
      65- }
      66- } else if (Array.isArray(prop.type) && !prop.type.includes('null')) {
      67- properties[propName] = {
      68- ...prop,
      69- type: [...prop.type, 'null'],
      70- }
      71- }
      72- }
      59+ } else if (wasOptional) {
      60+ // Make optional fields nullable by adding null to the type
      61+ if (prop.type && !Array.isArray(prop.type)) {
      62+ properties[propName] = {
      63+ ...prop,
      64+ type: [prop.type, 'null'],
      65+ }
      66+ } else if (Array.isArray(prop.type) && !prop.type.includes('null')) {
      67+ properties[propName] = {
      68+ ...prop,
      69+ type: [...prop.type, 'null'],
      70+ }
      71+ } else if (!prop.type && (prop.oneOf || prop.anyOf || prop.allOf)) {
      72+ // For schema composition, wrap in anyOf with null
      73+ properties[propName] = {
      74+ ...prop,
      75+ anyOf: [...(prop.anyOf || [prop]), { type: 'null' }],
      76+ }
      77+ }
      78+ }
      🤖 Prompt for AI Agents
      In packages/typescript/ai/src/activities/text/tools/zod-converter.ts around
      lines 59 to 72, the optional-to-nullable logic assumes prop.type exists; add a
      fallback branch for when prop.type is undefined so optional fields without an
      explicit type become nullable. Specifically, inside the wasOptional branch,
      detect if prop.type is missing and set properties[propName] = { ...prop, type:
      ['null'] } (so the schema allows null); keep the existing branches for
      single-type and array-type cases unchanged.
    9. tannerlinsley reviewed

    10. Actionable comments posted: 11

      ♻️ Duplicate comments (3)
      packages/typescript/ai/src/types.ts (2)

      572-578: Past concern: Clarify relationship between outputSchema and output fields.

      This issue was previously flagged. The ambiguity between output?: TOutput (line 571) and outputSchema?: z.ZodType (line 578) still exists, with no clear precedence or validation when both are provided.


      794-798: Past concern: Standardize token usage field naming.

      This issue was previously flagged. The usage object in ImageGenerationResult uses inputTokens and outputTokens, while other result types use promptTokens and completionTokens. This naming inconsistency should be addressed.

      docs/adapters/openai.md (1)

      147-161: Verify providerOptions parameters match the underlying API.

      This issue was flagged in a previous review. The example uses max_tokens, frequency_penalty, presence_penalty, and stop which are Chat Completions API parameters. If the underlying adapter uses the Responses API (client.responses.create()), these should be updated to valid Responses API parameters like max_output_tokens.

      #!/bin/bash
      # Check which OpenAI API endpoint the text adapter uses
      rg -n "responses\.create|chat\.completions\.create" packages/typescript/ai-openai/src/adapters/text.ts
      🧹 Nitpick comments (15)
      packages/typescript/ai/src/activities/transcription/adapter.ts (2)

      61-74: Make ID generation robust (avoid Math.random() / collisions).

      generateId() is collision-prone and not ideal for tracing. Prefer crypto.randomUUID() (with an isomorphic fallback) and consider keeping it stable-format for logs/telemetry.

       export abstract class BaseTranscriptionAdapter<
         TModels extends ReadonlyArray<string> = ReadonlyArray<string>,
         TProviderOptions extends object = Record<string, unknown>,
       > implements TranscriptionAdapter<TModels, TProviderOptions> {
      @@
      -  protected generateId(): string {
      -    return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`
      -  }
      +  protected generateId(): string {
      +    const uuid =
      +      typeof crypto !== 'undefined' && 'randomUUID' in crypto
      +        ? crypto.randomUUID()
      +        : `${Date.now()}-${Math.random().toString(16).slice(2)}`
      +    return `${this.name}-${uuid}`
      +  }
       }

      61-65: Consider readonly config to prevent accidental mutation.

      -  protected config: TranscriptionAdapterConfig
      +  protected readonly config: TranscriptionAdapterConfig
      packages/typescript/ai/src/activities/transcription/index.ts (2)

      44-62: Avoid duplicating TranscriptionOptions shape (drift risk).

      TranscriptionActivityOptions re-specifies fields that already exist in TranscriptionOptions (audio, language, prompt, responseFormat, providerOptions). Reuse the shared type so future changes don’t silently diverge.

      -import type { TranscriptionResult } from '../../types'
      +import type { TranscriptionOptions, TranscriptionResult } from '../../types'
      @@
       export interface TranscriptionActivityOptions<
         TAdapter extends TranscriptionAdapter<ReadonlyArray<string>, object>,
         TModel extends TranscriptionModels<TAdapter>,
      -> {
      +> extends Omit<
      +    TranscriptionOptions<TranscriptionProviderOptions<TAdapter>>,
      +    'model'
      +  > {
         /** The transcription adapter to use */
         adapter: TAdapter & { kind: typeof kind }
         /** The model name (autocompletes based on adapter) */
         model: TModel
      -  /** The audio data to transcribe - can be base64 string, File, Blob, or Buffer */
      -  audio: string | File | Blob | ArrayBuffer
      -  /** The language of the audio in ISO-639-1 format (e.g., 'en') */
      -  language?: string
      -  /** An optional prompt to guide the transcription */
      -  prompt?: string
      -  /** The format of the transcription output */
      -  responseFormat?: 'json' | 'text' | 'srt' | 'verbose_json' | 'vtt'
      -  /** Provider-specific options for transcription */
      -  providerOptions?: TranscriptionProviderOptions<TAdapter>
       }

      109-118: Optional: add a runtime kind assert for better DX on mis-wired adapters.

      Type checks can be bypassed with any; a simple runtime guard would produce a clearer error than “transcribe is not a function”.

      packages/typescript/ai-client/src/chat-client.ts (1)

      472-476: Consider adding safeguards for continuation chains.

      The current design allows indefinite continuation: stream → drain → checkForContinuation → new stream → repeat. While this enables multi-turn tool calling, it could theoretically loop indefinitely if tool execution patterns create cycles.

      Consider adding:

      • A maximum continuation depth counter
      • A circuit breaker if the same tool fails repeatedly
      • Logging/telemetry to track continuation chain length

      This is not an immediate issue but may help prevent edge-case runaway scenarios in production.

      packages/typescript/ai/src/types.ts (2)

      1054-1056: Consider renaming chatStream to align with Text-based terminology.

      The AIAdapter.chatStream method retains "chat" terminology while the rest of the codebase has been renamed to use "Text" (e.g., TextOptions, TextCompletionChunk, TextStreamOptionsUnion).

      If AIAdapter is the legacy interface being maintained for backwards compatibility, this is acceptable. However, if it's part of the new API surface, consider renaming to textStream for consistency.


      1240-1283: Consider refactoring to reduce code duplication.

      The TextOptionsForTextAdapter type (lines 1240-1283) duplicates the logic from TextStreamOptionsForModel (lines 1128-1183). Both types follow the same pattern:

      • Omit the same fields from TextOptions
      • Apply identical providerOptions narrowing
      • Use the same messages constraining logic based on ModelInputModalities

      Consider extracting this pattern into a shared utility type to improve maintainability:

      type BaseTextOptionsForAdapter<
        TAdapter,
        TModel extends string,
        ModelProviderOptions,
        ModelInputModalities,
        MessageMetadata
      > = Omit<TextOptions, 'model' | 'providerOptions' | 'responseFormat' | 'messages'> & {
        adapter: TAdapter
        model: TModel
        providerOptions?: TModel extends keyof ModelProviderOptions
          ? ModelProviderOptions[TModel]
          : never
        messages: // ... shared logic
      }

      Then both types could be defined more concisely by extracting the appropriate generics from their respective adapter interfaces.

      packages/typescript/ai-gemini/src/model-meta.ts (1)

      782-793: Consider reordering type alias after the const declaration for consistency.

      GeminiImageModels type alias (line 784) is defined before GEMINI_IMAGE_MODELS const (line 786), while GeminiModels (line 782) follows the pattern of being defined after GEMINI_MODELS (line 771). While TypeScript allows forward references in type positions, consistent ordering improves readability.

       export type GeminiModels = (typeof GEMINI_MODELS)[number]
       
      -export type GeminiImageModels = (typeof GEMINI_IMAGE_MODELS)[number]
      -
       export const GEMINI_IMAGE_MODELS = [
         GEMINI_2_5_FLASH_IMAGE.name,
         GEMINI_2_FLASH_IMAGE.name,
         IMAGEN_3.name,
         IMAGEN_4_GENERATE.name,
         IMAGEN_4_GENERATE_FAST.name,
         IMAGEN_4_GENERATE_ULTRA.name,
       ] as const
      +
      +export type GeminiImageModels = (typeof GEMINI_IMAGE_MODELS)[number]
      packages/typescript/ai/src/activities/tts/index.ts (1)

      55-57: Consider making format type more flexible for provider extensibility.

      The format option is hardcoded to OpenAI-specific formats ('mp3' | 'opus' | 'aac' | 'flac' | 'wav' | 'pcm'). Gemini TTS may support different formats. Consider extracting this from the adapter type or using a more generic string type with provider-specific constraints.

         /** The output audio format */
      -  format?: 'mp3' | 'opus' | 'aac' | 'flac' | 'wav' | 'pcm'
      +  format?: string

      Alternatively, extract format types from the adapter similar to how models are extracted, allowing each provider to define their supported formats.

      packages/typescript/ai-openai/src/audio/transcription-provider-options.ts (2)

      12-17: Naming convention: timestamp_granularities uses snake_case.

      Per coding guidelines, camelCase should be used for function and variable names. However, timestamp_granularities uses snake_case to match OpenAI's API. This is a reasonable trade-off for API consistency, but consider documenting this decision or using camelCase (timestampGranularities) with internal conversion.

      If maintaining snake_case for API parity is intentional (common pattern when matching external APIs), consider adding a brief comment explaining this choice.


      1-18: Consider expanding provider options for parity with OpenAI API.

      The interface only exposes temperature and timestamp_granularities. The OpenAI transcription API supports additional useful options like language, prompt, and response_format (referenced in the relevant snippet from transcribe-provider-options.ts). Consider if these should be exposed for full API coverage, or document that this is an intentionally minimal interface.

      Would you like me to help expand this interface with additional OpenAI transcription options?

      packages/typescript/ai-gemini/src/index.ts (1)

      5-71: Export parity: consider adding a GeminiTTSModel type re-export (missing today). You export GeminiTTSModels (value) but don’t appear to export the corresponding model-name type (parity with GeminiTextModel / GeminiImageModel). This is a small DX gap for consumers wanting model autocomplete for TTS.

      Proposed patch:

       export { GEMINI_TTS_MODELS as GeminiTTSModels } from './model-meta'
       export type { GeminiModels as GeminiTextModel } from './model-meta'
       export type { GeminiImageModels as GeminiImageModel } from './model-meta'
      +export type { GeminiTTSModels as GeminiTTSModel } from './model-meta'
      packages/typescript/ai/src/activities/video/index.ts (1)

      114-226: Return type isn’t wired to request (you define VideoActivityResult but don’t use it). Right now videoActivity always returns Promise<VideoJobResult | VideoStatusResult | VideoUrlResult>, so callers don’t get a type-safe result keyed by request.

      One way to connect the pieces:

      -export async function videoActivity<
      +export async function videoActivity<
         TAdapter extends VideoAdapter<ReadonlyArray<string>, object>,
         TModel extends VideoModels<TAdapter>,
      +  TRequest extends 'create' | 'status' | 'url' = 'create',
       >(
      -  options:
      -    | VideoCreateOptions<TAdapter, TModel>
      -    | VideoStatusOptions<TAdapter, TModel>
      -    | VideoUrlOptions<TAdapter, TModel>,
      -): Promise<VideoJobResult | VideoStatusResult | VideoUrlResult> {
      +  options: VideoActivityOptions<TAdapter, TModel, TRequest>,
      +): VideoActivityResult<TRequest> {
         const { adapter, request = 'create' } = options
         switch (request) {
           case 'status': {
      -      const statusOptions = options as VideoStatusOptions<TAdapter, TModel>
      -      return adapter.getVideoStatus(statusOptions.jobId)
      +      return adapter.getVideoStatus((options as VideoStatusOptions<TAdapter, TModel>).jobId) as any
           }
           case 'url': {
      -      const urlOptions = options as VideoUrlOptions<TAdapter, TModel>
      -      return adapter.getVideoUrl(urlOptions.jobId)
      +      return adapter.getVideoUrl((options as VideoUrlOptions<TAdapter, TModel>).jobId) as any
           }
           case 'create':
           default: {
      -      const createOptions = options as VideoCreateOptions<TAdapter, TModel>
      +      const createOptions = options as VideoCreateOptions<TAdapter, TModel>
             return adapter.createVideoJob({
               model: createOptions.model,
               prompt: createOptions.prompt,
               size: createOptions.size,
               duration: createOptions.duration,
               providerOptions: createOptions.providerOptions,
      -      })
      +      }) as any
           }
         }
       }
      packages/typescript/ai-openai/src/adapters/tts.ts (1)

      55-79: Avoid as OpenAITTSVoice/OpenAITTSFormat casts; validate or type the public API. Today voice/format can be any string at the @tanstack/ai layer, and the cast defeats type safety. If you want OpenAI-specific typing, consider exposing OpenAI-specific option types (or runtime guards) so invalid values fail predictably before the API call.

      packages/typescript/ai-openai/src/video/video-provider-options.ts (1)

      88-118: toApiSeconds can produce invalid OpenAIVideoSeconds unless callers validate first. Right now toApiSeconds(5) returns '5' as OpenAIVideoSeconds. Consider either (a) folding validation into conversion, or (b) making the validation+conversion helper the “one true” path.

      Example option:

      -export function toApiSeconds(
      -  seconds: number | string | undefined,
      -): OpenAIVideoSeconds | undefined {
      -  if (seconds === undefined) return undefined
      -  return String(seconds) as OpenAIVideoSeconds
      -}
      +export function toApiSeconds(
      +  model: string,
      +  seconds: number | string | undefined,
      +): OpenAIVideoSeconds | undefined {
      +  validateVideoSeconds(model, seconds)
      +  if (seconds === undefined) return undefined
      +  return String(seconds) as OpenAIVideoSeconds
      +}
      📜 Review details

      Configuration used: CodeRabbit UI

      Review profile: CHILL

      Plan: Pro

      📥 Commits

      Reviewing files that changed from the base of the PR and between 96a08b1bbf6eac1e77b23958812a79c859461085 and 2b56f66920772b963a07fa08c26d90e47e10c69c.

      ⛔ Files ignored due to path filters (1)
      • packages/typescript/smoke-tests/adapters/fixtures/test-audio.mp3 is excluded by !**/*.mp3
      📒 Files selected for processing (31)
      • docs/adapters/gemini.md (4 hunks)
      • docs/adapters/openai.md (9 hunks)
      • docs/config.json (4 hunks)
      • docs/guides/text-to-speech.md (1 hunks)
      • docs/guides/transcription.md (1 hunks)
      • docs/guides/video-generation.md (1 hunks)
      • packages/typescript/ai-client/src/chat-client.ts (4 hunks)
      • packages/typescript/ai-gemini/src/adapters/tts.ts (1 hunks)
      • packages/typescript/ai-gemini/src/index.ts (1 hunks)
      • packages/typescript/ai-gemini/src/model-meta.ts (10 hunks)
      • packages/typescript/ai-openai/src/adapters/transcription.ts (1 hunks)
      • packages/typescript/ai-openai/src/adapters/tts.ts (1 hunks)
      • packages/typescript/ai-openai/src/adapters/video.ts (1 hunks)
      • packages/typescript/ai-openai/src/audio/transcription-provider-options.ts (1 hunks)
      • packages/typescript/ai-openai/src/audio/tts-provider-options.ts (1 hunks)
      • packages/typescript/ai-openai/src/index.ts (1 hunks)
      • packages/typescript/ai-openai/src/model-meta.ts (7 hunks)
      • packages/typescript/ai-openai/src/video/video-provider-options.ts (1 hunks)
      • packages/typescript/ai-react/src/use-chat.ts (1 hunks)
      • packages/typescript/ai-solid/src/use-chat.ts (1 hunks)
      • packages/typescript/ai-svelte/src/create-chat.svelte.ts (1 hunks)
      • packages/typescript/ai-vue/src/use-chat.ts (1 hunks)
      • packages/typescript/ai/src/activities/index.ts (1 hunks)
      • packages/typescript/ai/src/activities/transcription/adapter.ts (1 hunks)
      • packages/typescript/ai/src/activities/transcription/index.ts (1 hunks)
      • packages/typescript/ai/src/activities/tts/adapter.ts (1 hunks)
      • packages/typescript/ai/src/activities/tts/index.ts (1 hunks)
      • packages/typescript/ai/src/activities/video/adapter.ts (1 hunks)
      • packages/typescript/ai/src/activities/video/index.ts (1 hunks)
      • packages/typescript/ai/src/ai.ts (1 hunks)
      • packages/typescript/ai/src/types.ts (11 hunks)
      ✅ Files skipped from review due to trivial changes (3)
      • docs/guides/transcription.md
      • docs/guides/video-generation.md
      • packages/typescript/ai-svelte/src/create-chat.svelte.ts
      🚧 Files skipped from review as they are similar to previous changes (2)
      • packages/typescript/ai-openai/src/model-meta.ts
      • packages/typescript/ai/src/activities/index.ts
      🧰 Additional context used
      📓 Path-based instructions (5)
      **/*.{ts,tsx}

      📄 CodeRabbit inference engine (CLAUDE.md)

      **/*.{ts,tsx}: Use tree-shakeable adapter architecture for provider implementations - export specialized adapters (text, embedding, summarize, image) as separate imports from /adapters subpath rather than monolithic adapters Use Zod for runtime schema validation and type inference, particularly for tool input/output definitions with toolDefinition() and Zod schema inference Implement isomorphic tool system using toolDefinition() with .server() and .client() implementations for dual-environment execution Use type-safe per-model configuration with provider options typed based on selected model to ensure compile-time safety Implement stream processing with StreamProcessor for handling chunked responses and support partial JSON parsing for streaming AI responses

      Files:

      • packages/typescript/ai-vue/src/use-chat.ts
      • packages/typescript/ai/src/activities/video/index.ts
      • packages/typescript/ai-openai/src/audio/tts-provider-options.ts
      • packages/typescript/ai-openai/src/audio/transcription-provider-options.ts
      • packages/typescript/ai/src/activities/tts/index.ts
      • packages/typescript/ai-react/src/use-chat.ts
      • packages/typescript/ai/src/activities/tts/adapter.ts
      • packages/typescript/ai-openai/src/video/video-provider-options.ts
      • packages/typescript/ai-client/src/chat-client.ts
      • packages/typescript/ai-gemini/src/adapters/tts.ts
      • packages/typescript/ai-gemini/src/model-meta.ts
      • packages/typescript/ai-openai/src/adapters/transcription.ts
      • packages/typescript/ai/src/activities/transcription/adapter.ts
      • packages/typescript/ai/src/activities/transcription/index.ts
      • packages/typescript/ai/src/types.ts
      • packages/typescript/ai-openai/src/adapters/tts.ts
      • packages/typescript/ai/src/activities/video/adapter.ts
      • packages/typescript/ai-openai/src/adapters/video.ts
      • packages/typescript/ai-solid/src/use-chat.ts
      • packages/typescript/ai/src/ai.ts
      • packages/typescript/ai-gemini/src/index.ts
      • packages/typescript/ai-openai/src/index.ts
      **/*.{ts,tsx,js,jsx}

      📄 CodeRabbit inference engine (CLAUDE.md)

      Use camelCase for function and variable names throughout the codebase

      Files:

      • packages/typescript/ai-vue/src/use-chat.ts
      • packages/typescript/ai/src/activities/video/index.ts
      • packages/typescript/ai-openai/src/audio/tts-provider-options.ts
      • packages/typescript/ai-openai/src/audio/transcription-provider-options.ts
      • packages/typescript/ai/src/activities/tts/index.ts
      • packages/typescript/ai-react/src/use-chat.ts
      • packages/typescript/ai/src/activities/tts/adapter.ts
      • packages/typescript/ai-openai/src/video/video-provider-options.ts
      • packages/typescript/ai-client/src/chat-client.ts
      • packages/typescript/ai-gemini/src/adapters/tts.ts
      • packages/typescript/ai-gemini/src/model-meta.ts
      • packages/typescript/ai-openai/src/adapters/transcription.ts
      • packages/typescript/ai/src/activities/transcription/adapter.ts
      • packages/typescript/ai/src/activities/transcription/index.ts
      • packages/typescript/ai/src/types.ts
      • packages/typescript/ai-openai/src/adapters/tts.ts
      • packages/typescript/ai/src/activities/video/adapter.ts
      • packages/typescript/ai-openai/src/adapters/video.ts
      • packages/typescript/ai-solid/src/use-chat.ts
      • packages/typescript/ai/src/ai.ts
      • packages/typescript/ai-gemini/src/index.ts
      • packages/typescript/ai-openai/src/index.ts
      packages/typescript/*/src/adapters/*.ts

      📄 CodeRabbit inference engine (CLAUDE.md)

      Create individual adapter implementations for each provider capability (text, embed, summarize, image) with separate exports to enable tree-shaking

      Files:

      • packages/typescript/ai-gemini/src/adapters/tts.ts
      • packages/typescript/ai-openai/src/adapters/transcription.ts
      • packages/typescript/ai-openai/src/adapters/tts.ts
      • packages/typescript/ai-openai/src/adapters/video.ts
      packages/typescript/*/src/model-meta.ts

      📄 CodeRabbit inference engine (CLAUDE.md)

      Maintain model metadata files that define provider options and capabilities per model for per-model type safety

      Files:

      • packages/typescript/ai-gemini/src/model-meta.ts
      packages/typescript/*/src/index.ts

      📄 CodeRabbit inference engine (CLAUDE.md)

      Export tree-shakeable adapters with clear subpath exports in package.json (e.g., @tanstack/ai/adapters, @tanstack/ai-openai/adapters) to minimize bundle size

      Files:

      • packages/typescript/ai-gemini/src/index.ts
      • packages/typescript/ai-openai/src/index.ts
      🧠 Learnings (10)
      📓 Common learnings
      Learnt from: CR
      Repo: TanStack/ai PR: 0
      File: CLAUDE.md:0-0
      Timestamp: 2025-12-13T17:09:09.783Z
      Learning: Applies to packages/typescript/*/src/adapters/*.ts : Create individual adapter implementations for each provider capability (text, embed, summarize, image) with separate exports to enable tree-shaking
      Learnt from: CR
      Repo: TanStack/ai PR: 0
      File: CLAUDE.md:0-0
      Timestamp: 2025-12-13T17:09:09.783Z
      Learning: Applies to packages/typescript/*/src/index.ts : Export tree-shakeable adapters with clear subpath exports in package.json (e.g., `tanstack/ai/adapters`, `tanstack/ai-openai/adapters`) to minimize bundle size
      Learnt from: CR
      Repo: TanStack/ai PR: 0
      File: CLAUDE.md:0-0
      Timestamp: 2025-12-13T17:09:09.783Z
      Learning: Applies to **/*.{ts,tsx} : Use tree-shakeable adapter architecture for provider implementations - export specialized adapters (text, embedding, summarize, image) as separate imports from `/adapters` subpath rather than monolithic adapters
      Learnt from: CR
      Repo: TanStack/ai PR: 0
      File: CLAUDE.md:0-0
      Timestamp: 2025-12-13T17:09:09.784Z
      Learning: Implement framework integrations using the headless `tanstack/ai-client` for state management with framework-specific hooks (useChat) on top
      Learnt from: CR
      Repo: TanStack/ai PR: 0
      File: CLAUDE.md:0-0
      Timestamp: 2025-12-13T17:09:09.784Z
      Learning: Ensure provider-agnostic design where the core AI SDK remains independent of any specific AI provider implementation
      📚 Learning: 2025-12-13T17:09:09.783Z
      Learnt from: CR
      Repo: TanStack/ai PR: 0
      File: CLAUDE.md:0-0
      Timestamp: 2025-12-13T17:09:09.783Z
      Learning: Applies to packages/typescript/*/src/adapters/*.ts : Create individual adapter implementations for each provider capability (text, embed, summarize, image) with separate exports to enable tree-shaking

      Applied to files:

      • packages/typescript/ai/src/activities/video/index.ts
      • packages/typescript/ai-openai/src/audio/tts-provider-options.ts
      • packages/typescript/ai/src/activities/tts/index.ts
      • docs/guides/text-to-speech.md
      • packages/typescript/ai/src/activities/tts/adapter.ts
      • packages/typescript/ai-gemini/src/adapters/tts.ts
      • docs/adapters/openai.md
      • packages/typescript/ai-openai/src/adapters/transcription.ts
      • packages/typescript/ai/src/activities/transcription/adapter.ts
      • packages/typescript/ai/src/activities/transcription/index.ts
      • packages/typescript/ai/src/types.ts
      • packages/typescript/ai-openai/src/adapters/tts.ts
      • packages/typescript/ai/src/activities/video/adapter.ts
      • packages/typescript/ai-openai/src/adapters/video.ts
      • packages/typescript/ai/src/ai.ts
      • packages/typescript/ai-gemini/src/index.ts
      • docs/adapters/gemini.md
      • packages/typescript/ai-openai/src/index.ts
      📚 Learning: 2025-12-13T17:09:09.783Z
      Learnt from: CR
      Repo: TanStack/ai PR: 0
      File: CLAUDE.md:0-0
      Timestamp: 2025-12-13T17:09:09.783Z
      Learning: Applies to packages/typescript/*/src/index.ts : Export tree-shakeable adapters with clear subpath exports in package.json (e.g., `tanstack/ai/adapters`, `tanstack/ai-openai/adapters`) to minimize bundle size

      Applied to files:

      • packages/typescript/ai/src/activities/video/index.ts
      • packages/typescript/ai/src/activities/tts/index.ts
      • packages/typescript/ai-react/src/use-chat.ts
      • packages/typescript/ai/src/activities/tts/adapter.ts
      • packages/typescript/ai-gemini/src/adapters/tts.ts
      • docs/adapters/openai.md
      • packages/typescript/ai-openai/src/adapters/transcription.ts
      • packages/typescript/ai/src/activities/transcription/adapter.ts
      • packages/typescript/ai/src/activities/transcription/index.ts
      • packages/typescript/ai/src/types.ts
      • packages/typescript/ai-openai/src/adapters/tts.ts
      • packages/typescript/ai/src/activities/video/adapter.ts
      • packages/typescript/ai-openai/src/adapters/video.ts
      • packages/typescript/ai/src/ai.ts
      • packages/typescript/ai-gemini/src/index.ts
      • docs/adapters/gemini.md
      • packages/typescript/ai-openai/src/index.ts
      📚 Learning: 2025-12-13T17:09:09.783Z
      Learnt from: CR
      Repo: TanStack/ai PR: 0
      File: CLAUDE.md:0-0
      Timestamp: 2025-12-13T17:09:09.783Z
      Learning: Applies to **/*.{ts,tsx} : Use tree-shakeable adapter architecture for provider implementations - export specialized adapters (text, embedding, summarize, image) as separate imports from `/adapters` subpath rather than monolithic adapters

      Applied to files:

      • packages/typescript/ai/src/activities/video/index.ts
      • packages/typescript/ai/src/activities/tts/index.ts
      • packages/typescript/ai/src/activities/tts/adapter.ts
      • packages/typescript/ai-gemini/src/adapters/tts.ts
      • docs/adapters/openai.md
      • packages/typescript/ai-openai/src/adapters/transcription.ts
      • packages/typescript/ai/src/activities/transcription/adapter.ts
      • packages/typescript/ai/src/activities/transcription/index.ts
      • packages/typescript/ai/src/types.ts
      • packages/typescript/ai-openai/src/adapters/tts.ts
      • packages/typescript/ai/src/activities/video/adapter.ts
      • packages/typescript/ai-openai/src/adapters/video.ts
      • packages/typescript/ai/src/ai.ts
      • packages/typescript/ai-gemini/src/index.ts
      • docs/adapters/gemini.md
      • packages/typescript/ai-openai/src/index.ts
      📚 Learning: 2025-12-13T17:09:09.783Z
      Learnt from: CR
      Repo: TanStack/ai PR: 0
      File: CLAUDE.md:0-0
      Timestamp: 2025-12-13T17:09:09.783Z
      Learning: Applies to packages/typescript/*/src/model-meta.ts : Maintain model metadata files that define provider options and capabilities per model for per-model type safety

      Applied to files:

      • packages/typescript/ai-openai/src/audio/tts-provider-options.ts
      • packages/typescript/ai-openai/src/audio/transcription-provider-options.ts
      • packages/typescript/ai/src/activities/tts/index.ts
      • packages/typescript/ai/src/activities/tts/adapter.ts
      • packages/typescript/ai-openai/src/video/video-provider-options.ts
      • packages/typescript/ai-gemini/src/model-meta.ts
      • packages/typescript/ai/src/activities/transcription/adapter.ts
      • packages/typescript/ai/src/types.ts
      • packages/typescript/ai-gemini/src/index.ts
      • packages/typescript/ai-openai/src/index.ts
      📚 Learning: 2025-12-13T17:09:09.784Z
      Learnt from: CR
      Repo: TanStack/ai PR: 0
      File: CLAUDE.md:0-0
      Timestamp: 2025-12-13T17:09:09.784Z
      Learning: Implement framework integrations using the headless `tanstack/ai-client` for state management with framework-specific hooks (useChat) on top

      Applied to files:

      • packages/typescript/ai-react/src/use-chat.ts
      • docs/adapters/openai.md
      • docs/adapters/gemini.md
      📚 Learning: 2025-12-13T17:09:09.783Z
      Learnt from: CR
      Repo: TanStack/ai PR: 0
      File: CLAUDE.md:0-0
      Timestamp: 2025-12-13T17:09:09.783Z
      Learning: Applies to **/*.{ts,tsx} : Use type-safe per-model configuration with provider options typed based on selected model to ensure compile-time safety

      Applied to files:

      • packages/typescript/ai/src/activities/tts/adapter.ts
      • packages/typescript/ai-openai/src/video/video-provider-options.ts
      • packages/typescript/ai-gemini/src/model-meta.ts
      • packages/typescript/ai/src/activities/transcription/adapter.ts
      • packages/typescript/ai/src/types.ts
      • packages/typescript/ai/src/activities/video/adapter.ts
      📚 Learning: 2025-12-13T17:09:09.783Z
      Learnt from: CR
      Repo: TanStack/ai PR: 0
      File: CLAUDE.md:0-0
      Timestamp: 2025-12-13T17:09:09.783Z
      Learning: Applies to **/*.{ts,tsx} : Implement stream processing with StreamProcessor for handling chunked responses and support partial JSON parsing for streaming AI responses

      Applied to files:

      • packages/typescript/ai-client/src/chat-client.ts
      • packages/typescript/ai/src/types.ts
      📚 Learning: 2025-12-13T17:09:09.783Z
      Learnt from: CR
      Repo: TanStack/ai PR: 0
      File: CLAUDE.md:0-0
      Timestamp: 2025-12-13T17:09:09.783Z
      Learning: Applies to **/*.{ts,tsx} : Implement isomorphic tool system using `toolDefinition()` with `.server()` and `.client()` implementations for dual-environment execution

      Applied to files:

      • docs/adapters/openai.md
      • packages/typescript/ai/src/types.ts
      • docs/adapters/gemini.md
      📚 Learning: 2025-12-13T17:09:09.783Z
      Learnt from: CR
      Repo: TanStack/ai PR: 0
      File: CLAUDE.md:0-0
      Timestamp: 2025-12-13T17:09:09.783Z
      Learning: Applies to **/*.{ts,tsx} : Use Zod for runtime schema validation and type inference, particularly for tool input/output definitions with `toolDefinition()` and Zod schema inference

      Applied to files:

      • docs/adapters/openai.md
      • packages/typescript/ai/src/types.ts
      • docs/adapters/gemini.md
      🧬 Code graph analysis (9)
      packages/typescript/ai-openai/src/audio/transcription-provider-options.ts (2)
      packages/typescript/ai-openai/src/index.ts (1)
      • OpenAITranscriptionProviderOptions (81-81)
      packages/typescript/ai-openai/src/audio/transcribe-provider-options.ts (1)
      • TranscribeProviderOptions (1-77)
      packages/typescript/ai/src/activities/tts/adapter.ts (1)
      packages/typescript/ai/src/types.ts (2)
      • TTSOptions (876-889)
      • TTSResult (894-907)
      packages/typescript/ai-openai/src/video/video-provider-options.ts (1)
      packages/typescript/ai-openai/src/index.ts (4)
      • OpenAIVideoSize (57-57)
      • OpenAIVideoProviderOptions (55-55)
      • OpenAIVideoModelProviderOptionsByName (56-56)
      • OpenAIVideoDuration (58-58)
      packages/typescript/ai-gemini/src/model-meta.ts (2)
      packages/typescript/ai-gemini/src/text/text-provider-options.ts (3)
      • GeminiSafetyOptions (16-23)
      • GeminiGenerationConfigOptions (25-168)
      • GeminiCachedContentOptions (170-175)
      packages/typescript/ai-gemini/src/index.ts (5)
      • GeminiModels (69-69)
      • GEMINI_MODELS (66-66)
      • GeminiImageModels (70-70)
      • GEMINI_IMAGE_MODELS (67-67)
      • GEMINI_TTS_MODELS (68-68)
      packages/typescript/ai-openai/src/adapters/transcription.ts (6)
      packages/typescript/ai/src/activities/index.ts (1)
      • BaseTranscriptionAdapter (247-247)
      packages/typescript/ai/src/activities/transcription/index.ts (1)
      • BaseTranscriptionAdapter (125-125)
      packages/typescript/ai-openai/src/model-meta.ts (1)
      • OPENAI_TRANSCRIPTION_MODELS (1720-1725)
      packages/typescript/ai-openai/src/audio/transcription-provider-options.ts (1)
      • OpenAITranscriptionProviderOptions (4-18)
      packages/typescript/ai/src/types.ts (3)
      • TranscriptionOptions (917-932)
      • TranscriptionResult (967-982)
      • TranscriptionSegment (937-950)
      packages/typescript/ai/src/activities/transcription/adapter.ts (1)
      • generateId (71-73)
      packages/typescript/ai/src/activities/transcription/adapter.ts (1)
      packages/typescript/ai/src/types.ts (2)
      • TranscriptionOptions (917-932)
      • TranscriptionResult (967-982)
      packages/typescript/ai-openai/src/adapters/tts.ts (4)
      packages/typescript/ai-openai/src/model-meta.ts (1)
      • OPENAI_TTS_MODELS (1711-1715)
      packages/typescript/ai-openai/src/audio/tts-provider-options.ts (2)
      • OpenAITTSVoice (4-15)
      • OpenAITTSFormat (20-20)
      packages/typescript/ai/src/types.ts (2)
      • TTSOptions (876-889)
      • TTSResult (894-907)
      packages/typescript/ai-openai/src/audio/audio-provider-options.ts (3)
      • validateAudioInput (71-75)
      • validateSpeed (56-62)
      • validateInstructions (64-69)
      packages/typescript/ai/src/activities/video/adapter.ts (4)
      packages/typescript/ai/src/activities/index.ts (3)
      • VideoAdapterConfig (211-211)
      • VideoAdapter (210-210)
      • BaseVideoAdapter (209-209)
      packages/typescript/ai/src/activities/video/index.ts (3)
      • VideoAdapterConfig (229-229)
      • VideoAdapter (229-229)
      • BaseVideoAdapter (230-230)
      packages/typescript/ai/src/ai.ts (1)
      • VideoAdapter (313-313)
      packages/typescript/ai/src/types.ts (4)
      • VideoGenerationOptions (811-824)
      • VideoJobResult (831-836)
      • VideoStatusResult (843-852)
      • VideoUrlResult (859-866)
      packages/typescript/ai/src/ai.ts (4)
      packages/typescript/ai/src/activities/index.ts (13)
      • GenerateAdapter (292-292)
      • TextAdapter (128-128)
      • EmbeddingAdapter (149-149)
      • SummarizeAdapter (168-168)
      • ImageAdapter (188-188)
      • VideoAdapter (210-210)
      • AnyAdapter (295-302)
      • TextModels (120-120)
      • EmbeddingModels (143-143)
      • SummarizeModels (162-162)
      • ImageModels (181-181)
      • VideoModels (201-201)
      • AnyAIAdapter (319-330)
      packages/typescript/ai/src/index.ts (3)
      • GenerateAdapter (6-6)
      • AnyAdapter (5-5)
      • ai (3-3)
      packages/typescript/ai/src/types.ts (7)
      • TextAdapter (1286-1286)
      • EmbeddingAdapter (1286-1286)
      • SummarizeAdapter (1286-1286)
      • AnyAdapter (1286-1286)
      • VideoStatusResult (843-852)
      • VideoUrlResult (859-866)
      • VideoJobResult (831-836)
      packages/typescript/ai/src/activities/video/adapter.ts (1)
      • VideoAdapter (32-65)
      🔇 Additional comments (34)
      packages/typescript/ai-react/src/use-chat.ts (1)

      83-91: LGTM! Behavioral improvement prevents premature request abortion.

      Removing isLoading from the effect dependencies is correct and prevents the cleanup from running whenever loading state toggles. This ensures that continuation requests and streaming responses aren't aborted mid-flight. The cleanup now properly runs only on unmount or when the client instance changes, which aligns with React best practices for resource lifecycle management.

      The comments clearly document the reasoning and warn against reintroducing the dependency.

      packages/typescript/ai/src/activities/transcription/index.ts (1)

      81-90: [Rewritten review comment] [Classification tag]

      packages/typescript/ai-client/src/chat-client.ts (3)

      29-29: LGTM! Clean queue declaration.

      The post-stream action queue is properly typed and initialized.


      401-407: LGTM! Deferred continuation prevents nested streams.

      The conditional queueing correctly prevents checkForContinuation from triggering a new stream while one is in progress.


      443-449: LGTM! Consistent deferred continuation pattern.

      The queueing logic mirrors the approach in addToolResult, maintaining consistency across tool-related operations.

      packages/typescript/ai-vue/src/use-chat.ts (1)

      42-44: LGTM! Unconditional cleanup is more robust.

      Removing the isLoading guard and unconditionally calling client.stop() on disposal is a good practice. This prevents potential resource leaks from edge cases where streaming activity exists but isLoading might not accurately reflect it. The stop() method is idempotent—it checks if an abort controller exists before aborting and safely handles multiple calls.

      Vue, React, and Solid all follow this same unconditional cleanup pattern. Note that Svelte intentionally handles cleanup differently (manual stop() calls only) due to Svelte 5 runes limitations, as documented in the implementation.

      packages/typescript/ai/src/types.ts (3)

      748-982: LGTM - Well-structured multimodal types.

      The new Image Generation (lines 748-799), Video Generation (lines 801-867), TTS (lines 868-907), and Transcription (lines 909-982) types are well-structured with comprehensive JSDoc documentation. The experimental markers on Video types appropriately signal the feature's status.


      1209-1286: LGTM - Consistent adapter extraction types.

      The new adapter extraction types (ExtractModelsFromTextAdapter, ExtractModelsFromEmbeddingAdapter, ExtractModelsFromSummarizeAdapter, ExtractModelsFromAnyAdapter) follow consistent patterns and properly support the tree-shakeable adapter architecture outlined in the coding guidelines. The type-only re-exports are appropriately structured.

      Based on learnings, these extraction types enable tree-shakeable adapter architecture by allowing model extraction from specialized adapters.


      7-12: The adapter types are properly implemented and exported. All four adapter types (TextAdapter, EmbeddingAdapter, SummarizeAdapter, AnyAdapter) are correctly defined in their respective adapter modules and exported from ./activities/index.ts, matching the import statement in types.ts. The tree-shakeable adapter architecture is correctly structured.

      packages/typescript/ai-solid/src/use-chat.ts (1)

      65-73: Cleanup improvement looks good—cleanup behavior is effectively consistent across all framework adapters.

      Removing the isLoading guard ensures that stop() is always called on component unmount, which is the correct behavior since the stop() method should be idempotent. Any in-flight requests should be cancelled when the component disposes, regardless of the current loading state. The added comments accurately describe the SolidJS disposal lifecycle.

      Cleanup patterns across frameworks are functionally equivalent:

      • React: useEffect with [client] dependency (cleanup on unmount or when client changes, though client only changes when clientId changes, which is rare)
      • SolidJS: createEffect without dependencies (cleanup on unmount only)
      • Vue: onScopeDispose (cleanup on scope disposal)
      • Svelte: Manual cleanup required due to framework constraints

      The slight differences in dependency arrays reflect framework-specific lifecycle patterns but result in the same practical behavior: cleanup runs on unmount, and stop() is reliably called to cancel in-flight requests.

      packages/typescript/ai-openai/src/audio/tts-provider-options.ts (1)

      1-31: LGTM!

      The TTS provider options type definitions are well-structured with appropriate JSDoc documentation. The types align with OpenAI's TTS API and follow the tree-shakeable adapter pattern established in the codebase.

      docs/guides/text-to-speech.md (1)

      1-248: LGTM!

      Comprehensive and well-structured TTS documentation that covers both OpenAI and Gemini providers. The code examples are consistent with the new tree-shakeable adapter architecture, and the best practices section provides valuable guidance.

      packages/typescript/ai-gemini/src/model-meta.ts (1)

      797-801: Experimental TTS models appropriately documented.

      The hardcoded model string is acceptable here given that the full model metadata constant (GEMINI_2_5_FLASH_TTS) is commented out as experimental. The @experimental JSDoc tag clearly communicates this status.

      packages/typescript/ai/src/activities/tts/index.ts (1)

      107-118: LGTM!

      Clean activity implementation with proper delegation to the adapter and appropriate type re-exports for the tree-shakeable architecture.

      docs/config.json (2)

      72-80: LGTM!

      Navigation entries for Text-to-Speech and Transcription guides are correctly added and follow the existing structure.


      174-179: The reference targets exist: docs/reference/functions/text.md and docs/reference/functions/textOptions.md are both present in the repository.

      packages/typescript/ai/src/activities/video/adapter.ts (2)

      32-65: LGTM!

      Well-designed video adapter interface with proper support for async job-based workflows. The generic parameters provide good type safety, and the @experimental tags appropriately communicate the feature's status.


      73-101: LGTM!

      The BaseVideoAdapter abstract class follows established patterns with proper use of declare for type-only properties and a useful generateId utility. The implementation provides a solid foundation for provider-specific adapters.

      packages/typescript/ai-gemini/src/index.ts (1)

      76-83: Deprecation messaging looks clear + keeps back-compat. The @deprecated block points users to the new split adapters while preserving GeminiAdapter exports for existing users. As per coding guidelines, this aligns with the tree-shakeable adapter architecture.

      packages/typescript/ai-openai/src/adapters/tts.ts (1)

      132-169: Factories match the established pattern. createOpenaiTTS + env-based openaiTTS are consistent with other adapters and keep the tree-shakeable surface clean.

      packages/typescript/ai/src/activities/tts/adapter.ts (1)

      1-72: Nice, minimal base contract for TTS adapters. Clear discriminator (kind: 'tts') + typed provider options hook.

      packages/typescript/ai-gemini/src/adapters/tts.ts (2)

      155-192: Factory helpers look consistent with other Gemini adapters. Explicit-key + env-key factory pair is clean and tree-shakeable. As per coding guidelines, this matches the split adapter architecture.


      86-101: TTS wiring is correct and matches current @google/genai SDK behavior.

      The responseModalities: ['AUDIO'] and speechConfig.voiceConfig.prebuiltVoiceConfig.voiceName structure conform to the official SDK documentation. Response handling correctly extracts audio via candidates[0].content.parts and processes inlineData for audio content.

      packages/typescript/ai/src/activities/video/index.ts (1)

      194-226: No action required—field names match the VideoAdapter contract.

      The videoActivity function correctly passes duration to adapter.createVideoJob(), which aligns with the VideoGenerationOptions interface definition. The field is properly documented as "Video duration in seconds" in the type definition, so there is no naming inconsistency or risk of drifting from provider quirks.

      packages/typescript/ai-openai/src/adapters/transcription.ts (1)

      49-125: The verbose_json cast is necessary and correct — the issue is incomplete OpenAI SDK typings, not incompatibility.

      The OpenAI SDK v6.9.1 typings only define Transcription { text: string }, but the actual verbose_json response includes duration, language, segments (with id, start, end, text, avg_logprob), and words (with word, start, end) per the official OpenAI API specification. The inline cast is a reasonable workaround for this known typings gap. The field mappings (avg_logprob → confidence via Math.exp()) align with the API schema. The real brittleness risk is if OpenAI's API response shape changes, not SDK type drift; consider extracting the cast to a dedicated type file or adding a comment explaining why the cast exists.

      packages/typescript/ai-openai/src/index.ts (1)

      1-112: Well-structured tree-shakeable exports.

      The organization with clear section comments and consistent export patterns aligns well with the coding guidelines for tree-shakeable adapter architecture. The @experimental JSDoc tag on Video adapter appropriately signals API instability.

      docs/adapters/openai.md (2)

      59-91: Comprehensive model documentation.

      The categorized model lists provide clear guidance for users selecting models for each capability.


      178-245: Well-documented new adapter capabilities.

      The Embeddings and Summarization sections provide clear examples including batch operations and provider options. The API patterns are consistent with the text adapter examples.

      packages/typescript/ai/src/ai.ts (3)

      72-157: Type mappings would need updates if TTS/Transcription are added.

      If the GenerateAdapter union is extended to include TTS and Transcription adapters, the AIOptionsFor and AIReturnFor conditional types will also need corresponding branches to handle these adapter kinds, otherwise they'll resolve to never.


      293-302: Clean routing implementation.

      The delegation pattern using activityMap is simple and extensible. The error message for unknown adapter kinds is helpful for debugging.


      308-339: Comprehensive re-exports for public API surface.

      The re-exports provide a clean public API, including both current adapter types and legacy aliases for backwards compatibility.

      packages/typescript/ai-openai/src/adapters/video.ts (3)

      144-153: Graceful 404 handling in status check.

      Returning a failed status instead of throwing on 404 is appropriate for polling scenarios where the job may have been cleaned up.


      303-323: Safe status mapping with sensible default.

      Defaulting unknown statuses to 'processing' is a reasonable choice for forward compatibility when OpenAI adds new status values.


      346-400: Consistent factory function pattern.

      The createOpenaiVideo and openaiVideo factory functions follow the same pattern established by other adapters (text, embed, image), maintaining API consistency across the package.

      docs/adapters/gemini.md lines 62 to 66OutdatedResolvedby AlemTuzlak
      docs/adapters/gemini.md lines 68 to 71OutdatedResolvedby AlemTuzlak
      @@ -323,3 +324,6 @@
      323324 } finally {
      324325 this.abortController = null
      325326 this.setIsLoading(false)
      327+
      328+ // Drain any actions that were queued while the stream was in progress
      329+ await this.drainPostStreamActions()
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      Consider clearing the queue when stop() is called.

      Currently, if a user calls stop() while tool results were queued, those actions will still execute when the finally block drains the queue. This means checkForContinuation could trigger a new stream immediately after the user explicitly stopped the current one.

      Consider this addition to the stop() method (line 359):

      stop(): void {
        if (this.abortController) {
          this.abortController.abort()
          this.abortController = null
        }
        this.postStreamActions = [] // Clear queued actions
        this.setIsLoading(false)
        this.events.stopped()
      }

      Similarly, consider clearing the queue in clear() (line 371) to prevent stale actions from executing after messages are cleared.

      🤖 Prompt for AI Agents
      In packages/typescript/ai-client/src/chat-client.ts around lines 327-329 (and
      specifically in the stop() implementation at ~line 359 and clear() at ~line
      371), queued postStreamActions are not cleared when a stream is aborted or
      messages are cleared, allowing those actions (and checkForContinuation) to run
      after the user stopped the stream; update stop() to reset this.postStreamActions
      = [] immediately after aborting the controller (and before firing
      stopped/setIsLoading), and likewise clear this.postStreamActions = [] in
      clear(), ensuring no stale queued actions execute after stop() or clear().
      @@ -441,7 +451,17 @@
      441451
      442452 /**
      443- * Continue the agent flow with current messages
      453+ * Queue an action to be executed after the current stream ends
      444454 */
      445- private async continueFlow(): Promise<void> {
      446- if (this.isLoading) return
      447- await this.streamResponse()
      455+ private queuePostStreamAction(action: () => Promise<void>): void {
      456+ this.postStreamActions.push(action)
      457+ }
      458+
      459+ /**
      460+ * Drain and execute all queued post-stream actions
      461+ */
      462+ private async drainPostStreamActions(): Promise<void> {
      463+ while (this.postStreamActions.length > 0) {
      464+ const action = this.postStreamActions.shift()!
      465+ await action()
      466+ }
      467+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟠 Major

      Add error handling in the drain loop.

      If a queued action throws an error, the exception will propagate and prevent subsequent actions from executing. This could leave the system in an inconsistent state where some continuations run but others don't.

      Apply this diff to handle errors gracefully:

       private async drainPostStreamActions(): Promise<void> {
         while (this.postStreamActions.length > 0) {
           const action = this.postStreamActions.shift()!
      -    await action()
      +    try {
      +      await action()
      +    } catch (error) {
      +      // Log but don't block remaining actions
      +      console.error('Post-stream action failed:', error)
      +      this.setError(error instanceof Error ? error : new Error(String(error)))
      +    }
         }
       }
      📝 Committable suggestion

      ‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

      Suggested change
      462- private async drainPostStreamActions(): Promise<void> {
      463- while (this.postStreamActions.length > 0) {
      464- const action = this.postStreamActions.shift()!
      465- await action()
      466- }
      467- }
      462+ private async drainPostStreamActions(): Promise<void> {
      463+ while (this.postStreamActions.length > 0) {
      464+ const action = this.postStreamActions.shift()!
      465+ try {
      466+ await action()
      467+ } catch (error) {
      468+ console.error('Post-stream action failed:', error)
      469+ }
      470+ }
      471+ }
      🤖 Prompt for AI Agents
      In packages/typescript/ai-client/src/chat-client.ts around lines 462 to 467, the
      drainPostStreamActions loop currently awaits each queued action directly which
      lets an exception stop the loop and skip remaining actions; wrap the await
      action() call in a try/catch so errors from a single action are caught, logged
      (use this.logger.error if available, otherwise console.error) and the loop
      continues to the next action; optionally collect failures into an array if
      callers need a summary, but do not let one action throw and abort the rest.
      @@ -0,0 +112,21 @@
      112+ const audioPart = parts.find((part: any) =>
      113+ part.inlineData?.mimeType?.startsWith('audio/'),
      114+ )
      115+
      116+ if (!audioPart || !('inlineData' in audioPart)) {
      117+ throw new Error('No audio data in Gemini TTS response')
      118+ }
      119+
      120+ const inlineData = (audioPart as any).inlineData
      121+ const audioBase64 = inlineData.data
      122+ const mimeType = inlineData.mimeType || 'audio/wav'
      123+ const format = mimeType.split('/')[1] || 'wav'
      124+
      125+ return {
      126+ id: generateId(this.name),
      127+ model,
      128+ audio: audioBase64,
      129+ format,
      130+ contentType: mimeType,
      131+ }
      132+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      Default voice can be skipped when voiceConfig is provided without voiceName. Right now voiceName: 'Kore' is only set when providerOptions?.voiceConfig is falsy. If callers pass { voiceConfig: { prebuiltVoiceConfig: {} } }, voiceName stays undefined.

      Possible adjustment:

      -    const voiceConfig = providerOptions?.voiceConfig || {
      -      prebuiltVoiceConfig: {
      -        voiceName: 'Kore', // Default Gemini voice
      -      },
      -    }
      +    const voiceConfig = {
      +      prebuiltVoiceConfig: {
      +        voiceName:
      +          providerOptions?.voiceConfig?.prebuiltVoiceConfig?.voiceName ?? 'Kore',
      +      },
      +      ...providerOptions?.voiceConfig,
      +    }

      Committable suggestion skipped: line range outside the PR's diff.

      🤖 Prompt for AI Agents
      In packages/typescript/ai-gemini/src/adapters/tts.ts around lines 78 to 132, the
      default voiceName 'Kore' is only applied when providerOptions?.voiceConfig is
      falsy, so if callers pass providerOptions.voiceConfig.prebuiltVoiceConfig but
      omit voiceName it remains undefined; update the voiceConfig construction to
      merge providerOptions.voiceConfig with a default prebuiltVoiceConfig.voiceName =
      'Kore' (i.e., if prebuiltVoiceConfig exists but has no voiceName, set it to
      'Kore'), ensuring you preserve any other provided fields.
      @@ -0,0 +90,21 @@
      90+
      91+ return {
      92+ id: generateId(this.name),
      93+ model,
      94+ audio: base64,
      95+ format: outputFormat,
      96+ contentType,
      97+ }
      98+ }
      99+
      100+ private getContentType(format: string): string {
      101+ const contentTypes: Record<string, string> = {
      102+ mp3: 'audio/mpeg',
      103+ opus: 'audio/opus',
      104+ aac: 'audio/aac',
      105+ flac: 'audio/flac',
      106+ wav: 'audio/wav',
      107+ pcm: 'audio/pcm',
      108+ }
      109+ return contentTypes[format] || 'audio/mpeg'
      110+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟠 Major

      🧩 Analysis chain

      🏁 Script executed:

      # Check package.json to see if this is an isomorphic package
      cat packages/typescript/ai-openai/package.json | head -50

      Repository: TanStack/ai

      Length of output: 1163


      🏁 Script executed:

      # Search for existing isomorphic base64 utilities in the repo
      rg "arrayBuffer.*[Bb]ase64|[Bb]ase64.*arrayBuffer" --type ts --type tsx

      Repository: TanStack/ai

      Length of output: 82


      🏁 Script executed:

      # Check if there are other Buffer usages in adapters that might indicate the pattern already established
      rg "Buffer\.from" --type ts --type tsx

      Repository: TanStack/ai

      Length of output: 82


      🏁 Script executed:

      # Check for any isomorphic utility files or helpers
      fd -e ts -e tsx | xargs grep -l "typeof Buffer\|btoa\|atob" | head -20

      Repository: TanStack/ai

      Length of output: 181


      🏁 Script executed:

      # Look at the actual file to see context
      cat -n packages/typescript/ai-openai/src/adapters/tts.ts | sed -n '45,120p'

      Repository: TanStack/ai

      Length of output: 2486


      🏁 Script executed:

      # Check transcription adapter for Buffer usage
      cat -n packages/typescript/ai-openai/src/adapters/transcription.ts | grep -A 3 -B 3 "Buffer"

      Repository: TanStack/ai

      Length of output: 509


      🏁 Script executed:

      # Look for utilities or helpers in the core @tanstack/ai package
      find packages/typescript/ai/src -type f -name "*util*" -o -name "*helper*" | head -20

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      # Search for any existing base64 or arrayBuffer handling in core package
      rg "base64|arrayBuffer|Buffer" packages/typescript/ai/src --type ts -A 2 -B 2 | head -50

      Repository: TanStack/ai

      Length of output: 3500


      🏁 Script executed:

      # Check if there's a shared utilities folder or isomorphic utils
      fd -e ts "util|helper|iso" packages/typescript/ai/src | head -20

      Repository: TanStack/ai

      Length of output: 37


      🏁 Script executed:

      # Check the build/vite config to see if there are any isomorphic/browser targets
      cat packages/typescript/ai-openai/vite.config.ts 2>/dev/null || cat packages/typescript/ai-openai/vite.config.js 2>/dev/null || echo "No vite config found"

      Repository: TanStack/ai

      Length of output: 827


      🏁 Script executed:

      # Check testing/panel useTTS hook
      cat -n testing/panel/src/hooks/useTTS.ts

      Repository: TanStack/ai

      Length of output: 2571


      🏁 Script executed:

      # Check the tts route component
      cat -n testing/panel/src/routes/tts.tsx | head -100

      Repository: TanStack/ai

      Length of output: 3505


      Use an isomorphic base64 conversion instead of Buffer.from().

      The adapter calls Buffer.from(arrayBuffer).toString('base64') at line 86, which will fail in browser environments. The testing panel's useTTS hook (line 39) expects base64-encoded audio from the TTS API endpoint, and while the current code runs server-side, this pattern prevents future isomorphic usage or client-side execution (e.g., in RSC or edge runtimes). Replace with an isomorphic helper:

      -    const arrayBuffer = await response.arrayBuffer()
      -    const base64 = Buffer.from(arrayBuffer).toString('base64')
      +    const arrayBuffer = await response.arrayBuffer()
      +    const base64 = arrayBufferToBase64(arrayBuffer)

      Add the helper function:

      +function arrayBufferToBase64(arrayBuffer: ArrayBuffer): string {
      +  if (typeof Buffer !== 'undefined') {
      +    return Buffer.from(arrayBuffer).toString('base64')
      +  }
      +  let binary = ''
      +  const bytes = new Uint8Array(arrayBuffer)
      +  for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]!)
      +  return btoa(binary)
      +}
      🤖 Prompt for AI Agents
      In packages/typescript/ai-openai/src/adapters/tts.ts around lines 52 to 110, the
      code uses Buffer.from(arrayBuffer).toString('base64') which breaks in
      browser/edge runtimes; replace that call with an isomorphic helper that converts
      an ArrayBuffer/Uint8Array to base64 (e.g., a small function that checks for
      globalThis.btoa and TextDecoder/Blob usage or falls back to Buffer in Node), add
      the helper in this file (or import from a shared utils) and call it like const
      base64 = arrayBufferToBase64(arrayBuffer); update the generateSpeech return to
      use the helper and ensure typing accepts ArrayBuffer | Uint8Array for robust
      conversion.
      @@ -0,0 +194,21 @@
      194+ ? new Date(videoInfo.expires_at)
      195+ : undefined,
      196+ }
      197+ }
      198+
      199+ // Last resort: The /content endpoint returns raw binary video data, not JSON.
      200+ // We need to construct a URL that the client can use to fetch the video.
      201+ // The URL needs to include auth, so we'll create a signed URL or return
      202+ // a proxy endpoint.
      203+
      204+ // For now, return a URL that goes through our API to proxy the request
      205+ // since the raw endpoint requires auth headers that browsers can't send.
      206+ // The video element can't add Authorization headers, so we need a workaround.
      207+
      208+ // Option 1: Return the direct URL (only works if OpenAI supports query param auth)
      209+ // Option 2: Return a blob URL after fetching (memory intensive)
      210+ // Option 3: Return a proxy URL through our server
      211+
      212+ // Let's try fetching and returning a data URL for now
      213+ const baseUrl = this.config.baseUrl || 'https://api.openai.com/v1'
      214+ const apiKey = this.config.apiKey
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      Verify config property name: baseUrl vs baseURL.

      Line 213 accesses this.config.baseUrl but OpenAI SDK configuration typically uses baseURL (uppercase URL). This could result in the fallback URL always defaulting to 'https://api.openai.com/v1' even when a custom base URL is configured.

      #!/bin/bash
      # Check the OpenAIClientConfig interface for the correct property name
      rg -n "baseUrl|baseURL" packages/typescript/ai-openai/src/utils.ts
      🤖 Prompt for AI Agents
      In packages/typescript/ai-openai/src/adapters/video.ts around lines 213 to 214,
      the code reads this.config.baseUrl which likely mismatches the
      OpenAIClientConfig property name (baseURL); update the access to use
      this.config.baseURL (or check the config interface and normalize both names) so
      a provided custom base URL is respected instead of always falling back to the
      hardcoded 'https://api.openai.com/v1'; ensure the change aligns with the
      OpenAIClientConfig definition in utils.ts and add a safe fallback if neither
      property is present.
      @@ -0,0 +232,21 @@
      232+ errorData.error?.message ||
      233+ `Failed to get video content: ${contentResponse.status}`,
      234+ )
      235+ }
      236+ throw new Error(
      237+ `Failed to get video content: ${contentResponse.status}`,
      238+ )
      239+ }
      240+
      241+ // The response is the raw video file - convert to base64 data URL
      242+ const videoBlob = await contentResponse.blob()
      243+ const buffer = await videoBlob.arrayBuffer()
      244+ const base64 = Buffer.from(buffer).toString('base64')
      245+ const mimeType =
      246+ contentResponse.headers.get('content-type') || 'video/mp4'
      247+
      248+ return {
      249+ jobId,
      250+ url: `data:${mimeType};base64,${base64}`,
      251+ expiresAt: undefined, // Data URLs don't expire
      252+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟠 Major

      Buffer is not available in browser environments.

      Buffer.from() is a Node.js API and will throw ReferenceError: Buffer is not defined in browser environments. For isomorphic code, consider using a cross-platform approach.

      -        const base64 = Buffer.from(buffer).toString('base64')
      +        // Cross-platform base64 encoding
      +        const base64 = typeof Buffer !== 'undefined'
      +          ? Buffer.from(buffer).toString('base64')
      +          : btoa(String.fromCharCode(...new Uint8Array(buffer)))

      Alternatively, consider using a utility like uint8array-extras or checking if video URL retrieval should be server-only.

      📝 Committable suggestion

      ‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

      Suggested change
      246+ // The response is the raw video file - convert to base64 data URL
      247+ const videoBlob = await contentResponse.blob()
      248+ const buffer = await videoBlob.arrayBuffer()
      249+ // Cross-platform base64 encoding
      250+ const base64 = typeof Buffer !== 'undefined'
      251+ ? Buffer.from(buffer).toString('base64')
      252+ : btoa(String.fromCharCode(...new Uint8Array(buffer)))
      253+ const mimeType =
      254+ contentResponse.headers.get('content-type') || 'video/mp4'
      255+
      256+ return {
      257+ jobId,
      258+ url: `data:${mimeType};base64,${base64}`,
      259+ expiresAt: undefined, // Data URLs don't expire
      260+ }
      🤖 Prompt for AI Agents
      In packages/typescript/ai-openai/src/adapters/video.ts around lines 241 to 252,
      the code uses Buffer.from(...) which breaks in browser environments; replace
      that Node-only call with an isomorphic conversion: after getting the
      ArrayBuffer, branch on whether globalThis.Buffer exists and use
      Buffer.from(buffer).toString('base64') for Node, otherwise convert the
      Uint8Array to a binary string (e.g. by iterating or chunking to avoid stack
      issues) and call btoa(binaryString) to produce the base64 string (or use
      FileReader.readAsDataURL to get a data URL in browsers), keep the existing
      mimeType handling and return shape, and ensure no Buffer references remain when
      running in the browser.
    11. Actionable comments posted: 18

      Caution

      Some comments are outside the diff and can’t be posted inline due to platform limitations.

      ⚠️ Outside diff range comments (2)
      packages/typescript/ai-openai/live-tests/tool-test-empty-object.ts (1)

      67-72: Use the public chat() function instead of calling adapter.chatStream() directly.

      The @ts-ignore comment indicates a typing issue. While chatStream is technically a public method on the adapter, the documented and recommended pattern is to use the chat() function from @tanstack/ai. Replace the direct adapter method call with:

      const stream = chat({
        adapter,
        model: 'gpt-4o-mini',
        messages,
        tools: [getGuitarsTool],
      })

      This approach follows the published API pattern and eliminates the need for type suppression.

      packages/typescript/ai-solid/README.md (1)

      1-3: Incorrect package name in README title.

      The title says @tanstack/ai-react but this file is located in the ai-solid package directory. This appears to be a copy-paste error that should be corrected.

      -# @tanstack/ai-react
      +# @tanstack/ai-solid
      
      -React hooks for building AI chat interfaces with TanStack AI.
      +Solid hooks for building AI chat interfaces with TanStack AI.
      ♻️ Duplicate comments (32)
      packages/typescript/ai/src/activities/chat/messages.ts (1)

      14-69: Previous review feedback not yet addressed.

      The issues raised in the prior review remain:

      1. Line 23: TSDoc example imports chat but doesn't use it — remove the unused import from the example
      2. Lines 63-66: msgs parameter should accept ReadonlyArray<...> to support as const inputs
      3. Lines 67-68: Add void _options to silence potential unused parameter warnings
      packages/typescript/ai/src/activities/chat/tools/zod-converter.ts (2)

      59-72: Handle edge case: optional fields without type property.

      The logic for making optional fields nullable assumes prop.type exists. However, JSON Schema properties can omit type when using oneOf, anyOf, or other combinators. If an optional field lacks a type property, it won't be made nullable, which could violate OpenAI's structured output requirements.

      Consider adding a fallback for properties without type:

             } else if (wasOptional) {
               // Make optional fields nullable by adding null to the type
               if (prop.type && !Array.isArray(prop.type)) {
                 properties[propName] = {
                   ...prop,
                   type: [prop.type, 'null'],
                 }
               } else if (Array.isArray(prop.type) && !prop.type.includes('null')) {
                 properties[propName] = {
                   ...prop,
                   type: [...prop.type, 'null'],
                 }
      +        } else if (!prop.type && (prop.oneOf || prop.anyOf || prop.allOf)) {
      +          // For schema composition, wrap in anyOf with null
      +          properties[propName] = {
      +            ...prop,
      +            anyOf: [...(prop.anyOf || [prop]), { type: 'null' }],
      +          }
               }
             }

      181-181: Update comment to reflect that toJSONSchema is part of Zod v4's standard API.

      The import import { toJSONSchema } from 'zod' is correct. However, the comment "Use Alcyone Labs fork which is compatible with Zod v4" is outdated. toJSONSchema is now part of Zod v4's standard API.

      Update the comment to reflect current reality:

      -  // Use Alcyone Labs fork which is compatible with Zod v4
      +  // Use Zod's built-in toJSONSchema for OpenAPI 3.0 compatibility
         const jsonSchema = toJSONSchema(schema, {
      packages/typescript/ai/src/activities/chat/adapter.ts (1)

      12-18: TextAdapterConfig properties remain unused (known issue).

      As noted in a previous review, the timeout, maxRetries, and headers properties in TextAdapterConfig are defined but not applied to SDK clients. This is a known issue that should be addressed in a follow-up.

      packages/typescript/ai/src/activities/embedding/index.ts (1)

      131-156: Error handling concern already raised in previous review.

      The unpaired lifecycle events issue (if adapter.createEmbeddings throws) has been flagged in a previous review comment. Wrapping the adapter call in try-catch with proper error event emission remains necessary for consistent event lifecycle tracking.

      docs/adapters/ollama.md (1)

      127-127: Hyphenate "tool-calling".

      This issue was already flagged in a previous review comment.

      -**Note:** Tool support varies by model. Models like `llama3`, `mistral`, and `qwen2` generally have good tool calling support.
      +**Note:** Tool support varies by model. Models like `llama3`, `mistral`, and `qwen2` generally have good tool-calling support.
      packages/typescript/ai-openai/src/adapters/transcription.ts (1)

      127-173: Critical: instanceof File/Blob can throw in environments without these globals.

      This issue was flagged in a previous review. The code uses instanceof File and instanceof Blob which will throw ReferenceError in Node.js environments where these globals don't exist. Additionally, atob may not be available.

      Guard with typeof checks before using instanceof, and provide fallback decoding:

         private prepareAudioFile(audio: string | File | Blob | ArrayBuffer): File {
           // If already a File, return it
      -    if (audio instanceof File) {
      +    if (typeof File !== 'undefined' && audio instanceof File) {
             return audio
           }
      
           // If Blob, convert to File
      -    if (audio instanceof Blob) {
      +    if (typeof Blob !== 'undefined' && audio instanceof Blob) {
             return new File([audio], 'audio.mp3', {
               type: audio.type || 'audio/mpeg',
             })
           }

      For atob, consider using Buffer.from(base64, 'base64') as a fallback in Node.js environments.

      packages/typescript/ai/src/activities/generateTranscription/index.ts (1)

      44-62: JSDoc/type mismatch: "Buffer" mentioned but not in type.

      The JSDoc at line 52 mentions "Buffer" as a valid audio input type, but the TypeScript type is string | File | Blob | ArrayBuffer. Either add Buffer to the type (if Node.js Buffer is supported) or remove "Buffer" from the JSDoc.

      packages/typescript/ai-ollama/src/adapters/summarize.ts (1)

      99-102: Existing review comment addresses the token/word semantic mismatch here.

      The past review already flagged that num_predict (tokens) and the prompt instruction (words) use maxLength inconsistently. Please address that feedback.

      docs/adapters/gemini.md (1)

      156-169: Verify embedding model name matches the adapter's supported models.

      The documentation uses gemini-embedding-001. A past review flagged inconsistencies between the embedding model lists in different files. Please ensure this model name matches what's defined in the Gemini embed adapter.

      #!/bin/bash
      # Check embedding model definitions
      echo "=== Gemini embedding models in adapter ==="
      rg -n "GeminiEmbeddingModels|GEMINI_EMBEDDING" packages/typescript/ai-gemini/src/
      
      echo ""
      echo "=== Model meta definitions ==="
      rg -B2 -A5 "GEMINI_EMBEDDING_MODELS" packages/typescript/ai-gemini/src/model-meta.ts
      packages/typescript/ai-gemini/src/adapters/tts.ts (1)

      103-107: Default voice can be skipped when voiceConfig is provided without voiceName.

      The current logic only sets the default voiceName: 'Kore' when modelOptions?.voiceConfig is entirely falsy. If a caller provides { voiceConfig: { prebuiltVoiceConfig: {} } }, voiceName will be undefined.

      Consider merging defaults more defensively:

      -    const voiceConfig = modelOptions?.voiceConfig || {
      -      prebuiltVoiceConfig: {
      -        voiceName: 'Kore' as const,
      -      },
      -    }
      +    const voiceConfig = {
      +      prebuiltVoiceConfig: {
      +        voiceName:
      +          modelOptions?.voiceConfig?.prebuiltVoiceConfig?.voiceName ?? ('Kore' as const),
      +      },
      +      ...modelOptions?.voiceConfig,
      +    }
      docs/adapters/openai.md (1)

      115-129: Verify modelOptions parameter names match the underlying API.

      The example uses max_tokens, frequency_penalty, presence_penalty, and stop which are Chat Completions API parameters. If the adapter uses the Responses API internally, these should be max_output_tokens (and some parameters may not be supported).

      #!/bin/bash
      # Check which API the OpenAI text adapter uses and what parameters it accepts
      rg -n "max_tokens|max_output_tokens|frequency_penalty" packages/typescript/ai-openai/src/adapters/text.ts -B 2 -A 2
      rg -n "responses\.create|chat\.completions" packages/typescript/ai-openai/src/adapters/text.ts
      packages/typescript/ai-ollama/src/adapters/text.ts (2)

      189-212: Ollama's ToolCall type lacks an id property—runtime risk.

      The type assertion on lines 190-193 assumes ToolCall has id and function.index, but Ollama v0.6.3's ToolCall only provides function.name and function.arguments. Line 201 uses actualToolCall.id without a fallback, risking undefined access.

      Consider using responseId as a fallback:

      -            id: actualToolCall.id,
      +            id: actualToolCall.id ?? `${responseId}-${chunk.message.tool_calls?.indexOf(toolCall) ?? 0}`,

      359-378: systemPrompts not forwarded to Ollama.

      The mapCommonOptionsToOllama function doesn't include options.systemPrompts in the returned ChatRequest. Unlike other adapters, system prompts are silently dropped. Ollama's API supports system role messages.

      +    // Prepend system messages if systemPrompts provided
      +    const formattedMessages = this.formatMessages(options.messages)
      +    const messagesWithSystem = options.systemPrompts?.length
      +      ? [
      +          { role: 'system' as const, content: options.systemPrompts.join('\n') },
      +          ...formattedMessages,
      +        ]
      +      : formattedMessages
      
           return {
             model,
             options: ollamaOptions,
      -      messages: this.formatMessages(options.messages),
      +      messages: messagesWithSystem,
             tools: this.convertToolsToOllamaFormat(options.tools),
           }
      packages/typescript/ai-openai/src/adapters/video.ts (2)

      213-214: Verify config property name: baseUrl vs baseURL.

      Line 213 accesses this.config.baseUrl but OpenAI SDK configuration typically uses baseURL (uppercase URL). This could result in the fallback URL always being used even when a custom base URL is configured.

      #!/bin/bash
      # Check the OpenAIClientConfig interface for the correct property name
      rg -n "baseUrl|baseURL" packages/typescript/ai-openai/src/utils/client.ts

      241-252: Buffer is not available in browser environments.

      Buffer.from() is a Node.js API and will throw ReferenceError: Buffer is not defined in browsers. For isomorphic code, use a cross-platform approach.

      -        const base64 = Buffer.from(buffer).toString('base64')
      +        // Cross-platform base64 encoding
      +        const uint8Array = new Uint8Array(buffer)
      +        const base64 = typeof Buffer !== 'undefined'
      +          ? Buffer.from(buffer).toString('base64')
      +          : btoa(Array.from(uint8Array, byte => String.fromCharCode(byte)).join(''))

      Alternatively, consider making this method server-only or using a utility library for consistent cross-platform encoding.

      packages/typescript/ai-anthropic/src/adapters/summarize.ts (1)

      51-61: AnthropicSummarizeProviderOptions is defined but not used.

      The interface defines temperature and maxTokens (lines 23-28), but the summarize method hardcodes temperature: 0.3 and ignores modelOptions. Users cannot customize these values.

      Wire the provider options into the implementation:

       async summarize(options: SummarizationOptions): Promise<SummarizationResult> {
         const systemPrompt = this.buildSummarizationPrompt(options)
      +  const providerOpts = (options as any).modelOptions as AnthropicSummarizeProviderOptions | undefined
      
         const response = await this.client.messages.create({
           model: options.model,
           messages: [{ role: 'user', content: options.text }],
           system: systemPrompt,
      -    max_tokens: options.maxLength || 500,
      -    temperature: 0.3,
      +    max_tokens: providerOpts?.maxTokens ?? options.maxLength ?? 500,
      +    temperature: providerOpts?.temperature ?? 0.3,
           stream: false,
         })
      packages/typescript/ai-gemini/src/adapters/text.ts (6)

      203-204: Tool call ID collision: function name is not unique across multiple calls.

      Using functionCall.name directly as the toolCallId causes collisions when the same function is called multiple times in one stream. Each tool call must have a unique identifier.

      Also applies to: 271-272


      252-263: Remove dead code handling non-existent chunk.data property.

      The chunk.data property does not exist on GenerateContentResponse in the @google/genai types. This branch will never execute.


      327-334: Incorrect token count mapping—use candidatesTokenCount instead of thoughtsTokenCount.

      thoughtsTokenCount represents the model's internal reasoning tokens (chain-of-thought), not completion tokens. Completion tokens should come from candidatesTokenCount, which represents the actual generated response.


      353-367: Incorrect default MIME type for non-image content.

      The fallback 'image/jpeg' is used for all media types including audio, video, and documents. This will cause issues when processing non-image content without explicit metadata.


      419-428: Use the actual function name, not toolCallId, for functionResponse.name.

      According to the Gemini API specification, the functionResponse.name field must be the functionDeclaration.name (the tool/function identifier), not a call ID.


      395-408: Unsafe type casting after failed JSON parse.

      When JSON parsing fails, the code casts toolCall.function.arguments to Record<string, unknown>, but if arguments is a string (which it likely is if parsing failed), this cast is incorrect and could cause runtime errors.

      packages/typescript/ai-openai/src/adapters/text.ts (5)

      88-97: Normalize headers to plain object before passing to OpenAI SDK, and use abortController.signal.

      TextOptions.request can contain a Headers object, but the OpenAI Node SDK expects headers to be a plain Record<string, string>. Additionally, options.abortController?.signal should be prioritized over options.request?.signal.

      Also applies to: 137-155


      230-230: Unused variable eventTypeCounts.

      This map is declared but never populated with event type counts. Either implement the counting logic or remove this variable and its reference in error logging at line 386.


      667-672: Base64 images need data URI format for OpenAI API.

      The code passes part.source.value directly to image_url, but for source.type === 'data', this is raw base64. OpenAI's Responses API expects a data URI like data:image/jpeg;base64,....


      674-687: Remove audio handling from Responses API adapter—not supported.

      The OpenAI Responses API does not support audio inputs at all. Audio must use the dedicated Audio API endpoints (transcriptions/translations/realtime). This code will fail at runtime.


      106-113: chatStream() should yield an error chunk instead of throwing.

      Throwing from the generator forces consumers to wrap the iteration in try/catch and prevents unified stream error handling. Other adapters yield error chunks instead.

      packages/typescript/ai-anthropic/src/adapters/text.ts (3)

      257-266: Spread of validProviderOptions can override systemPrompts.

      The system field is set explicitly from options.systemPrompts?.join('\n') at line 263, but since 'system' is included in validKeys (line 228), it can be extracted from providerOptions and will override the joined systemPrompts when spread at line 265.


      500-518: Use the index field from content_block_stop to identify which block stopped.

      The handler checks toolCallsMap.get(currentToolIndex) without verifying that the stopped block is actually a tool_use. Since content_block_stop events include an index field, use that to confirm the stopped block type.


      109-121: Error code may result in string "undefined".

      If both err.code and err.status are undefined, String(err.status) produces "undefined" as the error code, which isn't meaningful for consumers.

      Also applies to: 581-594

      packages/typescript/ai-openai/src/adapters/tts.ts (1)

      52-98: Use an isomorphic base64 conversion instead of Buffer.from().

      Line 86 uses Buffer.from(arrayBuffer).toString('base64') which will fail in browser and edge runtime environments. This prevents the adapter from being truly isomorphic.

      🧹 Nitpick comments (23)
      docs/reference/functions/toStreamResponse.md (1)

      39-50: Example correctly demonstrates the new tree-shakeable adapter API.

      The update from openai() to openaiText() properly reflects the refactoring to modular, provider-specific adapters. The example clearly shows how to use the text adapter within the chat() function.

      Consider adding an import statement for full clarity, especially if this guide is the primary reference for users learning the new API pattern:

       ```typescript
      +import { openaiText } from '@tanstack/ai-openai/adapters';
      +
       export async function POST(request: Request) {

      This helps users immediately understand where openaiText() is imported from and makes the example fully self-contained. Based on learnings, adapters should be exported with clear subpath exports.

      docs/reference/functions/combineStrategies.md (1)

      33-43: Add import statement to the example for clarity.

      The example uses openaiText() without showing the import path. Users need to know where to import this adapter from to use the example.

      Consider adding the import statement at the top of the code block:

       ```typescript
      +import { openaiText } from '@tanstack/ai-openai/adapters';
      +
       const stream = chat({
         adapter: openaiText(),

      Please verify: Is this the correct import path for the new tree-shakeable adapter exports? If the path differs, this should be updated to match the documented pattern.

      packages/typescript/ai/src/activities/generateVideo/adapter.ts (1)

      98-100: Consider using crypto.randomUUID() for more robust ID generation.

      The current implementation using Math.random().toString(36).substring(7) produces IDs with limited entropy and inconsistent length due to the substring(7) behavior (which may produce fewer than 6 characters if the random number is small).

        protected generateId(): string {
      -   return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`
      +   return `${this.name}-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`
        }

      Alternatively, if targeting modern runtimes, consider crypto.randomUUID() for standards-compliant unique IDs.

      packages/typescript/ai/src/activities/generateVideo/index.ts (2)

      83-106: The model parameter appears unused in status/URL operations.

      VideoStatusOptions and VideoUrlOptions extend VideoActivityBaseOptions which requires a model field, but the underlying adapter methods getVideoStatus(jobId) and getVideoUrl(jobId) only accept jobId. The model parameter becomes dead weight for these operations.

      Consider either:

      1. Creating a simpler base interface for status/URL operations without model:
      +interface VideoJobOptions<TAdapter extends VideoAdapter<ReadonlyArray<string>, object>> {
      +  adapter: TAdapter & { kind: typeof kind }
      +  jobId: string
      +}
      +
       export interface VideoStatusOptions<
         TAdapter extends VideoAdapter<ReadonlyArray<string>, object>,
      -  TModel extends VideoModels<TAdapter>,
      -> extends VideoActivityBaseOptions<TAdapter, TModel> {
      +> extends VideoJobOptions<TAdapter> {
         request: 'status'
      -  jobId: string
       }
      1. Or documenting that model is required for API consistency even if unused.

      234-242: Silent error swallowing may mask issues.

      When the video is completed but URL fetch fails, the function returns error but still reports status: 'completed'. This could confuse consumers who check status === 'completed' and expect a URL to be available.

      Consider returning a distinct status or throwing to make the failure explicit:

           } catch (error) {
      -      // If URL fetch fails, still return status
             return {
      -        status: statusResult.status,
      +        status: 'failed' as const,
               progress: statusResult.progress,
               error:
                 error instanceof Error ? error.message : 'Failed to get video URL',
             }
           }

      Alternatively, document the behavior clearly in JSDoc that status: 'completed' with error set indicates a URL retrieval failure.

      packages/typescript/ai/src/activities/chat/tools/zod-converter.ts (1)

      161-164: Consider renaming function to reflect dual input support.

      The function name convertZodToJsonSchema suggests it only handles Zod schemas, but it also accepts and passes through JSONSchema objects (line 170-178). While the documentation clarifies this, a more accurate name like convertSchemaToJsonSchema or normalizeSchema would better reflect the function's actual behavior.

      examples/ts-group-chat/chat-server/claude-service.ts (1)

      7-43: Consider using Zod for tool definitions per coding guidelines.

      While the JSONSchema approach is valid, the coding guidelines recommend using Zod for tool input/output definitions to leverage runtime validation and type inference. As an example file, demonstrating the recommended pattern would be beneficial.

      As per coding guidelines, "Use Zod for runtime schema validation and type inference, particularly for tool input/output definitions with toolDefinition() and Zod schema inference."

      Example refactor using Zod:

      import { z } from 'zod'
      
      const getWeatherTool = toolDefinition({
        name: 'getWeather',
        description: 'Get the current weather for a location. Returns temperature, conditions, and humidity.',
        inputSchema: z.object({
          location: z.string().describe('The city or location to get weather for'),
          unit: z.enum(['celsius', 'fahrenheit']).optional().describe('Temperature unit (defaults to celsius)'),
        }),
        outputSchema: z.object({
          location: z.string(),
          temperature: z.number(),
          unit: z.string(),
          conditions: z.string(),
          humidity: z.number().optional(),
        }),
      }).server((args) => {
        // Implementation remains the same
        // ...
      })
      packages/typescript/ai/src/activities/index.ts (2)

      27-34: Consider consolidating duplicate imports from ./chat/index.

      There are two separate import statements from './chat/index' requiring eslint-disable comments. These could be merged into a single import block.

       // Import model types for use in local type definitions
       import type {
         InputModalitiesForModel,
         MessageMetadataForAdapter,
         TextModels,
         TextProviderOptionsForModel,
      -  // eslint-disable-next-line import/no-duplicates
      +  TextActivityOptions,
      +  TextActivityResult,
       } from './chat/index'
      ...
      -// eslint-disable-next-line import/no-duplicates
      -import type { TextActivityOptions, TextActivityResult } from './chat/index'

      Also applies to: 84-85


      251-252: Consider stronger typing for ActivityHandler if this type will be retained.

      The any types lose type safety. If this type remains in use after deprecation cleanup, consider using generics or a more specific union type.

      packages/typescript/ai-openai/live-tests/tool-test-empty-object.ts (1)

      1-1: Import is correct; consider adding subpath exports for full tree-shaking benefits.

      The createOpenaiChat export is properly defined in packages/typescript/ai-openai/src/adapters/text.ts (line 743) and correctly re-exported from the package index. The function is typed to return OpenAITextAdapter and integrates well with the existing tree-shakeable adapter architecture.

      However, the adapter architecture could be optimized by configuring subpath exports in package.json (e.g., @tanstack/ai-openai/adapters) to allow consumers to import directly from adapter modules, further reducing bundle size for those only using specific adapters. Currently, all adapters are exported from the main entry point, which limits tree-shaking effectiveness.

      packages/typescript/ai/src/activities/generateSpeech/index.ts (1)

      44-62: Consider reusing TTSOptions fields to reduce duplication.

      The voice, format, and speed fields are duplicated from TTSOptions in types.ts. While the current approach works, consider using Pick or Omit to derive these from the base type for easier maintenance.

      // Alternative approach using Pick:
      export interface TTSActivityOptions<
        TAdapter extends TTSAdapter<ReadonlyArray<string>, object>,
        TModel extends TTSModels<TAdapter>,
      > extends Pick<TTSOptions<TTSProviderOptions<TAdapter>>, 'text' | 'voice' | 'format' | 'speed' | 'modelOptions'> {
        adapter: TAdapter & { kind: typeof kind }
        model: TModel
      }
      docs/guides/streaming.md (1)

      20-20: Consider preserving trailing commas for consistency.

      The trailing comma after messages was removed, which is inconsistent with common TypeScript style guides that recommend trailing commas for better diffs and easier reordering.

      Apply this diff to restore the trailing comma:

         model: "gpt-4o",
      -  messages,
      +  messages,
       });

      This pattern also applies to line 43.

      docs/guides/runtime-adapter-switching.md (1)

      82-82: Consider simplifying the wording.

      The phrase "exact same" could be replaced with "identical" or "same" for more concise writing.

      -`createChatOptions` is a simple identity function with the **exact same type signature** as `chat()`.
      +`createChatOptions` is a simple identity function with the **same type signature** as `chat()`.
      packages/typescript/ai-openai/src/tools/function-tool.ts (1)

      27-30: Potential issue with required property access.

      On line 29, inputSchema.required is accessed, but the JSONSchema type may not guarantee this property exists. While the default schema on lines 21-25 includes required: [], if tool.inputSchema is provided but lacks a required property, this could pass undefined to makeOpenAIStructuredOutputCompatible.

      Consider using the fallback consistently:

         const jsonSchema = makeOpenAIStructuredOutputCompatible(
           inputSchema,
      -    inputSchema.required || [],
      +    (inputSchema.required as string[] | undefined) ?? [],
         )
      examples/ts-react-chat/src/routes/api.tanchat.ts (2)

      22-45: Consider extracting model defaults to a configuration object.

      The adapterConfig pattern with inline createChatOptions calls is functional but couples model defaults to the adapter configuration. For better maintainability, consider extracting model defaults:

      const DEFAULT_MODELS = {
        anthropic: 'claude-sonnet-4-5',
        gemini: 'gemini-2.0-flash-exp',
        ollama: 'mistral:7b',
        openai: 'gpt-4o',
      } as const
      
      const adapterConfig = {
        anthropic: () =>
          createChatOptions({
            adapter: anthropicText(),
            model: DEFAULT_MODELS.anthropic,
          }),
        // ... etc
      }

      This makes it easier to update models without touching the adapter creation logic.


      105-121: The cast comment is misleading about union types.

      The comment states that TypeScript sees "a union of all possible return types", but chat() with properly typed adapters should return a well-typed stream. The real issue is likely that chat() is overloaded or has multiple signatures.

      Consider removing or clarifying the comment to explain why the cast is needed more accurately. If the cast is truly necessary, it might indicate a type definition issue in the core library that should be addressed.

      packages/typescript/ai/src/activities/generateTranscription/adapter.ts (1)

      50-74: Consider using crypto.randomUUID() for more robust ID generation.

      The generateId helper at line 71-73 uses Date.now() combined with a 5-character random suffix. While functional, this could produce collisions under high-frequency calls (same millisecond + ~1 in 60 million collision chance per call).

      For a library that may be used in high-throughput scenarios, consider using crypto.randomUUID() if available, or at minimum increasing the random suffix length.

        protected generateId(): string {
      -   return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`
      +   return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(2, 11)}`
        }
      packages/typescript/ai/src/activities/generateImage/index.ts (1)

      22-55: Consider replacing any with more specific type constraints in conditional types.

      The type helpers use any in infer positions (e.g., ImageAdapter<infer M, any, any, any>). While this works, using object or unknown would be more type-safe and consistent with the constraints used elsewhere in the file.

       export type ImageModels<TAdapter> =
      -  TAdapter extends ImageAdapter<infer M, any, any, any> ? M[number] : string
      +  TAdapter extends ImageAdapter<infer M, object, object, object> ? M[number] : string
      packages/typescript/ai/src/activities/summarize/adapter.ts (1)

      60-91: Same generateId pattern as transcription adapter.

      The generateId implementation has the same potential collision concern noted in the transcription adapter. Consider standardizing ID generation across all adapters, possibly extracting it to a shared utility.

        protected generateId(): string {
      -   return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`
      +   return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(2, 11)}`
        }
      packages/typescript/ai-openai/src/adapters/embed.ts (1)

      119-131: Consider adding deprecation notice or JSDoc to alias functions.

      openaiEmbed and createOpenaiEmbed are aliases for openaiEmbedding and createOpenaiEmbedding. If these are deprecated aliases, consider adding @deprecated JSDoc annotations. If they're intentional alternatives, brief JSDoc would clarify the relationship.

      +/**
      + * @deprecated Use openaiEmbedding() instead
      + */
       export function openaiEmbed(
         config?: Omit<OpenAIEmbedConfig, 'apiKey'>,
       ): OpenAIEmbedAdapter {
         const apiKey = getOpenAIApiKeyFromEnv()
         return createOpenaiEmbedding(apiKey, config)
       }
      
      +/**
      + * @deprecated Use createOpenaiEmbedding() instead
      + */
       export function createOpenaiEmbed(
      packages/typescript/ai-ollama/src/adapters/summarize.ts (1)

      80-89: Unused constructor parameter _options.

      The _options parameter is accepted but never used. Either wire it through to configure default model/behavior, or remove it to avoid confusion.

         constructor(
           hostOrClient?: string | Ollama,
      -    _options: OllamaSummarizeAdapterOptions = {},
         ) {

      Or use it:

         constructor(
           hostOrClient?: string | Ollama,
      -    _options: OllamaSummarizeAdapterOptions = {},
      +    private options: OllamaSummarizeAdapterOptions = {},
         ) {
      packages/typescript/ai-gemini/src/adapters/tts.ts (1)

      140-151: Consider narrowing types instead of using any casts.

      The any casts work around SDK type limitations but reduce type safety. Consider defining a local interface for the expected response shape:

      interface GeminiAudioPart {
        inlineData?: {
          mimeType?: string
          data?: string
        }
      }

      This makes the expected structure explicit and catches regressions if the SDK changes.

      packages/typescript/ai/src/activities/chat/index.ts (1)

      769-794: Consider improving type safety in collectClientState.

      Lines 771-772 use (message as any).parts which bypasses type checking. If parts is a valid property for certain message types, consider extending the ModelMessage type or using a type guard.

      // Type guard approach
      function hasToolCallParts(message: ModelMessage): message is ModelMessage & { parts: Array<any> } {
        return message.role === 'assistant' && 'parts' in message
      }
      @@ -151,3 +151,4 @@
      151151
      152152 for await (const chunk of chat({
      153153 adapter: this.adapter,
      154+ model: 'claude-sonnet-4-5',
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      Use a specific Claude Sonnet 4.5 version with snapshot date for production.

      Use specific model versions like claude-sonnet-4-5-20250929 in production applications to ensure consistent behavior, rather than the alias form 'claude-sonnet-4-5'. Aliases automatically point to the most recent model snapshot, and when new model snapshots are released, they are migrated to point to the newest version typically within a week, which can introduce unexpected behavior changes.

      🤖 Prompt for AI Agents
      In examples/ts-group-chat/chat-server/claude-service.ts around line 154, the
      code uses the alias 'claude-sonnet-4-5' which can float to newer snapshots;
      replace the alias with a specific snapshoted model name such as
      'claude-sonnet-4-5-YYYYMMDD' (use the desired snapshot date e.g., 20250929) to
      pin behavior in production, updating any related deployment/config docs and
      tests to reference the exact model string.
      @@ -151,5 +151,6 @@
      151151
      152152 for await (const chunk of chat({
      153153 adapter: this.adapter,
      154+ model: 'claude-sonnet-4-5',
      154155 systemPrompts: [systemMessage],
      155156 messages: [...conversationHistory] as any,
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟠 Major

      Remove type assertion and fix the type mismatch.

      The as any assertion bypasses type safety and suggests a type mismatch between Array<ModelMessage> and what the messages parameter expects. This could hide type errors and lead to runtime issues.

      Investigate the expected type for the messages parameter and either:

      1. Update the conversationHistory type annotation to match
      2. Transform the messages to the correct type
      3. If the types are actually compatible, use a proper type assertion instead of as any

      Example approach:

      messages: conversationHistory as ModelMessage[]

      Or if transformation is needed:

      messages: conversationHistory.map(msg => ({
        // Transform to expected format
      }))
      🤖 Prompt for AI Agents
      In examples/ts-group-chat/chat-server/claude-service.ts around line 156, remove
      the unsafe "as any" on messages: investigate the expected parameter type (e.g.,
      ModelMessage[]) and fix the mismatch by either (1) changing
      conversationHistory's type annotation to the expected type, (2) mapping
      conversationHistory to produce the exact shape the API expects (map each msg to
      the required fields), or (3) if they are already compatible, use a precise
      assertion like conversationHistory as ModelMessage[]; ensure the final value
      matches the expected property names and types instead of silencing the compiler
      with any.
      @@ -0,0 +76,21 @@
      76+ }
      77+ }
      78+
      79+ async *summarizeStream(
      80+ options: SummarizationOptions,
      81+ ): AsyncIterable<StreamChunk> {
      82+ const systemPrompt = this.buildSummarizationPrompt(options)
      83+ const id = generateId(this.name)
      84+ const model = options.model
      85+ let accumulatedContent = ''
      86+ let inputTokens = 0
      87+ let outputTokens = 0
      88+
      89+ const stream = await this.client.messages.create({
      90+ model: options.model,
      91+ messages: [{ role: 'user', content: options.text }],
      92+ system: systemPrompt,
      93+ max_tokens: options.maxLength || 500,
      94+ temperature: 0.3,
      95+ stream: true,
      96+ })
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      Stream version also ignores provider options.

      The summarizeStream method (line 94) similarly hardcodes temperature: 0.3 instead of using provider options. Both methods should be updated for consistency.

      🤖 Prompt for AI Agents
      In packages/typescript/ai-anthropic/src/adapters/summarize.ts around lines 79 to
      96, the streaming call hardcodes temperature: 0.3 and thus ignores provider
      options; update the request to use the temperature supplied by the summarization
      options (e.g. options.providerOptions?.temperature or options.temperature) with
      a fallback to 0.3, and spread any other providerOptions into the
      client.messages.create call so provider-specific settings are respected (keep
      existing max_tokens and other fields intact).
      @@ -29,11 +28,15 @@
      2928 const metadata =
      3029 (tool.metadata as { cacheControl?: CacheControl | null } | undefined) || {}
      3130
      32- // Convert Zod schema to JSON Schema
      33- const jsonSchema = convertZodToJsonSchema(tool.inputSchema)
      31+ // Tool schemas are already converted to JSON Schema in the ai layer
      32+ const jsonSchema = (tool.inputSchema ?? {
      33+ type: 'object',
      34+ properties: {},
      35+ required: [],
      36+ }) as JSONSchema
      3437
      3538 const inputSchema = {
      3639 type: 'object' as const,
      37- properties: jsonSchema?.properties || null,
      38- required: jsonSchema?.required || null,
      40+ properties: jsonSchema.properties || null,
      41+ required: jsonSchema.required || null,
      3942 }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      Potential type inconsistency with fallback schema.

      The fallback schema at lines 32-36 provides properties: {} and required: [], but the || null operator at lines 40-41 will preserve these as empty objects/arrays rather than converting them to null. This creates an inconsistency with the type definition at lines 20-21, which expects null for absent values.

      Consider one of these approaches:

      Option 1: Use null in the fallback (preferred for type consistency)

        const jsonSchema = (tool.inputSchema ?? {
          type: 'object',
      -   properties: {},
      -   required: [],
      +   properties: null,
      +   required: null,
        }) as JSONSchema

      Option 2: Remove || null if empty objects/arrays are acceptable

        const inputSchema = {
          type: 'object' as const,
      -   properties: jsonSchema.properties || null,
      -   required: jsonSchema.required || null,
      +   properties: jsonSchema.properties ?? null,
      +   required: jsonSchema.required ?? null,
        }
      📝 Committable suggestion

      ‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

      Suggested change
      31- // Tool schemas are already converted to JSON Schema in the ai layer
      32- const jsonSchema = (tool.inputSchema ?? {
      33- type: 'object',
      34- properties: {},
      35- required: [],
      36- }) as JSONSchema
      37-
      38- const inputSchema = {
      39- type: 'object' as const,
      40- properties: jsonSchema.properties || null,
      41- required: jsonSchema.required || null,
      42- }
      31+ // Tool schemas are already converted to JSON Schema in the ai layer
      32+ const jsonSchema = (tool.inputSchema ?? {
      33+ type: 'object',
      34+ properties: null,
      35+ required: null,
      36+ }) as JSONSchema
      37+
      38+ const inputSchema = {
      39+ type: 'object' as const,
      40+ properties: jsonSchema.properties || null,
      41+ required: jsonSchema.required || null,
      42+ }
      🤖 Prompt for AI Agents
      In packages/typescript/ai-anthropic/src/tools/custom-tool.ts around lines 31 to
      42, the fallback JSON schema sets properties: {} and required: [] but
      inputSchema then uses `|| null`, which leaves empty objects/arrays instead of
      null and causes a type mismatch; fix by making the fallback use null for absent
      values (e.g., properties: null and required: null) so inputSchema receives null
      consistently, or alternatively remove the `|| null` in inputSchema if empty
      objects/arrays are acceptable—preferred: change the fallback to null for both
      properties and required to preserve the intended types.
      @@ -0,0 +44,21 @@
      44+ super({})
      45+ this.textAdapter = new OpenAITextAdapter(config)
      46+ }
      47+
      48+ async summarize(options: SummarizationOptions): Promise<SummarizationResult> {
      49+ const systemPrompt = this.buildSummarizationPrompt(options)
      50+
      51+ // Use the text adapter's streaming and collect the result
      52+ let summary = ''
      53+ let id = ''
      54+ let model = options.model
      55+ let usage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 }
      56+
      57+ for await (const chunk of this.textAdapter.chatStream({
      58+ model: options.model,
      59+ messages: [{ role: 'user', content: options.text }],
      60+ systemPrompts: [systemPrompt],
      61+ options: {
      62+ maxTokens: options.maxLength,
      63+ temperature: 0.3,
      64+ },
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      Temperature from provider options is ignored.

      The OpenAISummarizeProviderOptions interface defines temperature and maxTokens, but in summarize() and summarizeStream(), temperature is hardcoded to 0.3 and options.modelOptions is not passed through. This means user-provided provider options have no effect.

           for await (const chunk of this.textAdapter.chatStream({
             model: options.model,
             messages: [{ role: 'user', content: options.text }],
             systemPrompts: [systemPrompt],
             options: {
      -        maxTokens: options.maxLength,
      -        temperature: 0.3,
      +        maxTokens: options.modelOptions?.maxTokens ?? options.maxLength,
      +        temperature: options.modelOptions?.temperature ?? 0.3,
             },
           })) {
      📝 Committable suggestion

      ‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

      Suggested change
      61- options: {
      62- maxTokens: options.maxLength,
      63- temperature: 0.3,
      64- },
      61+ options: {
      62+ maxTokens: options.modelOptions?.maxTokens ?? options.maxLength,
      63+ temperature: options.modelOptions?.temperature ?? 0.3,
      64+ },
      🤖 Prompt for AI Agents
      In packages/typescript/ai-openai/src/adapters/summarize.ts around lines 61 to
      64, the provider options are being ignored: temperature is hardcoded to 0.3 and
      modelOptions is not forwarded, so user-specified temperature,
      maxTokens/maxLength, and other modelOptions have no effect; update both
      summarize() and summarizeStream() to build the request options by merging
      options.modelOptions (if present) with explicit fields and by using
      options.temperature (fall back to a sensible default) and options.maxTokens or
      options.maxLength for max_tokens, ensuring the final payload uses the merged
      object passed to the OpenAI call and that types/defaults are respected.
      @@ -0,0 +74,21 @@
      74+ }
      75+
      76+ return { id, model, summary, usage }
      77+ }
      78+
      79+ async *summarizeStream(
      80+ options: SummarizationOptions,
      81+ ): AsyncIterable<StreamChunk> {
      82+ const systemPrompt = this.buildSummarizationPrompt(options)
      83+
      84+ // Delegate directly to the text adapter's streaming
      85+ yield* this.textAdapter.chatStream({
      86+ model: options.model,
      87+ messages: [{ role: 'user', content: options.text }],
      88+ systemPrompts: [systemPrompt],
      89+ options: {
      90+ maxTokens: options.maxLength,
      91+ temperature: 0.3,
      92+ },
      93+ })
      94+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      Same provider options issue applies to streaming.

      The summarizeStream method also ignores options.modelOptions. Apply the same fix to use provider options when available.

         async *summarizeStream(
           options: SummarizationOptions,
         ): AsyncIterable<StreamChunk> {
           const systemPrompt = this.buildSummarizationPrompt(options)
      
           yield* this.textAdapter.chatStream({
             model: options.model,
             messages: [{ role: 'user', content: options.text }],
             systemPrompts: [systemPrompt],
             options: {
      -        maxTokens: options.maxLength,
      -        temperature: 0.3,
      +        maxTokens: options.modelOptions?.maxTokens ?? options.maxLength,
      +        temperature: options.modelOptions?.temperature ?? 0.3,
             },
           })
         }
      📝 Committable suggestion

      ‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

      Suggested change
      78-
      79- async *summarizeStream(
      80- options: SummarizationOptions,
      81- ): AsyncIterable<StreamChunk> {
      82- const systemPrompt = this.buildSummarizationPrompt(options)
      83-
      84- // Delegate directly to the text adapter's streaming
      85- yield* this.textAdapter.chatStream({
      86- model: options.model,
      87- messages: [{ role: 'user', content: options.text }],
      88- systemPrompts: [systemPrompt],
      89- options: {
      90- maxTokens: options.maxLength,
      91- temperature: 0.3,
      78+ async *summarizeStream(
      79+ options: SummarizationOptions,
      80+ ): AsyncIterable<StreamChunk> {
      81+ const systemPrompt = this.buildSummarizationPrompt(options)
      82+
      83+ // Delegate directly to the text adapter's streaming
      84+ yield* this.textAdapter.chatStream({
      85+ model: options.model,
      86+ messages: [{ role: 'user', content: options.text }],
      87+ systemPrompts: [systemPrompt],
      88+ options: {
      89+ maxTokens: options.modelOptions?.maxTokens ?? options.maxLength,
      90+ temperature: options.modelOptions?.temperature ?? 0.3,
      91+ },
      92+ })
      93+ }
      🤖 Prompt for AI Agents
      In packages/typescript/ai-openai/src/adapters/summarize.ts around lines 79 to
      94, the summarizeStream method currently ignores options.modelOptions
      (provider-specific options) when delegating to textAdapter.chatStream; update
      the call to include/merge provider options by spreading options.modelOptions (or
      options.providerOptions if that naming is used) into the options passed to
      chatStream while keeping maxTokens and temperature, and ensure the model
      argument still falls back to options.model; this will pass provider-specific
      streaming settings through to the text adapter.
      @@ -0,0 +1,12 @@
      1+import type { TTSOptions, TTSResult } from '../../types'
      2+
      3+/**
      4+ * Configuration for TTS adapter instances
      5+ */
      6+export interface TTSAdapterConfig {
      7+ apiKey?: string
      8+ baseUrl?: string
      9+ timeout?: number
      10+ maxRetries?: number
      11+ headers?: Record<string, string>
      12+}
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      🧩 Analysis chain

      🏁 Script executed:

      #!/bin/bash
      # Find all TTS adapter files in the repository
      find . -type f -name "*tts*" -o -name "*speech*" | grep -E "\.(ts|tsx)$" | head -20

      Repository: TanStack/ai

      Length of output: 372


      🏁 Script executed:

      #!/bin/bash
      # Search for TTSAdapterConfig usage and inheritance
      rg "TTSAdapterConfig|TTSAdapter|BaseTTSAdapter" -t ts -t tsx --max-count 50

      Repository: TanStack/ai

      Length of output: 82


      🏁 Script executed:

      #!/bin/bash
      # Search for TTSAdapterConfig and related adapter code
      rg "TTSAdapterConfig|TTSAdapter|BaseTTSAdapter|timeout|maxRetries" -t ts --max-count 100 | grep -E "(tts|TTS|generateSpeech)" | head -50

      Repository: TanStack/ai

      Length of output: 4383


      🏁 Script executed:

      #!/bin/bash
      # Check the adapter.ts file structure
      cat packages/typescript/ai/src/activities/generateSpeech/adapter.ts

      Repository: TanStack/ai

      Length of output: 2145


      🏁 Script executed:

      #!/bin/bash
      # Check OpenAI TTS adapter implementation
      cat packages/typescript/ai-openai/src/adapters/tts.ts

      Repository: TanStack/ai

      Length of output: 4907


      🏁 Script executed:

      #!/bin/bash
      # Check Gemini TTS adapter implementation
      cat packages/typescript/ai-gemini/src/adapters/tts.ts

      Repository: TanStack/ai

      Length of output: 6605


      🏁 Script executed:

      #!/bin/bash
      # Check OpenAIClientConfig definition and usage
      rg "OpenAIClientConfig" -t ts -A 10 packages/typescript/ai-openai/src/utils.ts | head -50

      Repository: TanStack/ai

      Length of output: 137


      🏁 Script executed:

      #!/bin/bash
      # Check GeminiClientConfig definition and usage
      rg "GeminiClientConfig" -t ts -A 10 packages/typescript/ai-gemini/src/utils.ts | head -50

      Repository: TanStack/ai

      Length of output: 137


      🏁 Script executed:

      #!/bin/bash
      # Check createOpenAIClient implementation
      rg "createOpenAIClient" -t ts -A 15 packages/typescript/ai-openai/src/utils.ts

      Repository: TanStack/ai

      Length of output: 137


      🏁 Script executed:

      #!/bin/bash
      # Check createGeminiClient implementation
      rg "createGeminiClient" -t ts -A 15 packages/typescript/ai-gemini/src/utils.ts

      Repository: TanStack/ai

      Length of output: 137


      🏁 Script executed:

      #!/bin/bash
      # Find where createOpenAIClient is defined
      rg "export.*createOpenAIClient|function createOpenAIClient" -t ts --max-count 20

      Repository: TanStack/ai

      Length of output: 181


      🏁 Script executed:

      #!/bin/bash
      # Find where createGeminiClient is defined
      rg "export.*createGeminiClient|function createGeminiClient" -t ts --max-count 20

      Repository: TanStack/ai

      Length of output: 182


      🏁 Script executed:

      #!/bin/bash
      # List files in ai-openai src directory
      find packages/typescript/ai-openai/src -type f -name "*.ts" | head -30

      Repository: TanStack/ai

      Length of output: 1817


      🏁 Script executed:

      #!/bin/bash
      # List files in ai-gemini src directory
      find packages/typescript/ai-gemini/src -type f -name "*.ts" | head -30

      Repository: TanStack/ai

      Length of output: 1300


      🏁 Script executed:

      #!/bin/bash
      # Check OpenAI createOpenAIClient implementation
      cat packages/typescript/ai-openai/src/utils/client.ts

      Repository: TanStack/ai

      Length of output: 1219


      🏁 Script executed:

      #!/bin/bash
      # Check Gemini createGeminiClient implementation
      cat packages/typescript/ai-gemini/src/utils/client.ts

      Repository: TanStack/ai

      Length of output: 1190


      🏁 Script executed:

      #!/bin/bash
      # Check OpenAIClientConfig definition
      rg "interface OpenAIClientConfig|type OpenAIClientConfig" -t ts -A 10 packages/typescript/ai-openai/src/

      Repository: TanStack/ai

      Length of output: 1305


      🏁 Script executed:

      #!/bin/bash
      # Check GeminiClientConfig definition
      rg "interface GeminiClientConfig|type GeminiClientConfig" -t ts -A 10 packages/typescript/ai-gemini/src/

      Repository: TanStack/ai

      Length of output: 1003


      🏁 Script executed:

      #!/bin/bash
      # Double-check if timeout/maxRetries/headers are used anywhere in generateSpeech methods or elsewhere in the adapters
      rg "timeout|maxRetries|headers" packages/typescript/ai-openai/src/adapters/tts.ts packages/typescript/ai-gemini/src/adapters/tts.ts

      Repository: TanStack/ai

      Length of output: 37


      Confirm: TTSAdapterConfig properties (timeout, maxRetries, headers) are declared but not applied to SDK clients.

      The config interface declares these properties, but they are never extracted or passed to the underlying OpenAI or Gemini SDK clients. OpenAIClientConfig only supports apiKey, organization, and baseURL, while GeminiClientConfig only supports apiKey. Either implement these properties in the client initialization or remove them from the config interface to avoid confusion.

    12. jherr approved these changes

    13. AlemTuzlak merged this8d77614

    14. AlemTuzlak closed this

Sign in to join this conversation.