Patch Changes
-
#393
91b32e0Thanks @threepointone! - Comprehensive cleanup of the workers-ai-provider package.Bug fixes:
-
Fixed phantom dependency on
fetch-event-streamthat caused runtime crashes when installed outside the monorepo. Replaced with a built-in SSE parser. -
Fixed streaming buffering: responses now stream token-by-token instead of arriving all at once. The root cause was twofold — an eager
ReadableStreamstart()pattern that buffered all chunks, and a heuristic that silently fell back to non-streamingdoGeneratewhenever tools were defined. Both are fixed. Streaming now uses a properTransformStreampipeline with backpressure. -
Fixed
reasoning-deltaID mismatch in simulated streaming — was usinggenerateId()instead of thereasoningIdfrom the precedingreasoning-startevent, causing the AI SDK to drop reasoning content. -
Fixed REST API client (
createRun) silently swallowing HTTP errors. Non-200 responses now throw with status code and response body. -
Fixed
response_formatbeing sent asundefinedon every non-JSON request. Now only included when actually set. -
Fixed
json_schemafield evaluating tofalse(a boolean) instead ofundefinedwhen schema was missing.Workers AI quirk workarounds:
-
Added
sanitizeToolCallId()— strips non-alphanumeric characters and pads/truncates to 9 chars, fixing tool call round-trips through the binding which rejects its own generated IDs. -
Added
normalizeMessagesForBinding()— convertscontent: nullto""and sanitizes tool call IDs before every binding call. Only applied on the binding path (REST preserves original IDs). -
Added null-finalization chunk filtering for streaming tool calls.
-
Added numeric value coercion in native-format streams (Workers AI sometimes returns numbers instead of strings for the
responsefield). -
Improved image model to handle all output types from
binding.run():ReadableStream,Uint8Array,ArrayBuffer,Response, and{ image: base64 }objects. -
Graceful degradation: if
binding.run()returns a non-streaming response despitestream: true, it wraps the complete response as a simulated stream instead of throwing.Premature stream termination detection:
-
Streams that end without a
[DONE]sentinel now reportfinishReason: "error"withraw: "stream-truncated"instead of silently reporting"stop". -
Stream read errors are caught and emit
finishReason: "error"withraw: "stream-error".AI Search (formerly AutoRAG):
-
Added
createAISearchandAISearchChatLanguageModelas the canonical exports, reflecting the rename from AutoRAG to AI Search. -
createAutoRAGstill works but emits a one-time deprecation warning pointing tocreateAISearch. -
createAutoRAGpreserves"autorag.chat"as the provider name for backward compatibility. -
AI Search now warns when tools or JSON response format are provided (unsupported by the
aiSearchAPI). -
Simplified AI Search internals — removed dead tool/response-format processing code.
Code quality:
-
Removed dead code:
workersai-error.ts(never imported),workersai-image-config.ts(inlined). -
Consistent file naming: renamed
workers-ai-embedding-model.tstoworkersai-embedding-model.ts. -
Replaced
StringLikecatch-all index signatures with[key: string]: unknownon settings types. -
Replaced
anytypes with proper interfaces (FlatToolCall,OpenAIToolCall,PartialToolCall). -
Tightened
processToolCallformat detection to checkfunction.nameinstead of just the presence of afunctionproperty. -
Removed
@ai-sdk/provider-utilsandzodpeer dependencies (no longer used in source). -
Added
imageModelto theWorkersAIinterface type for consistency.Tests:
-
149 unit tests across 10 test files (up from 82).
-
New test coverage:
sanitizeToolCallId,normalizeMessagesForBinding,prepareToolsAndToolChoice,processText,mapWorkersAIUsage, image model output types, streaming error scenarios (malformed SSE, premature termination, empty stream), backpressure verification, graceful degradation (non-streaming fallback with text/tools/reasoning), REST API error handling (401/404/500), AI Search warnings, embeddingTooManyEmbeddingValuesForCallError, message conversion with images and reasoning. -
Integration tests for REST API and binding across 12 models and 7 categories (chat, streaming, multi-turn, tool calling, tool round-trip, structured output, image generation, embeddings).
-
All tests use the AI SDK's public APIs (
generateText,streamText,generateImage,embedMany) instead of internal.doGenerate()/.doStream()methods.README:
-
Rewritten from scratch with concise examples, model recommendations, configuration guide, and known limitations section.
-
Updated to use current AI SDK v6 APIs (
generateText+Output.objectinstead of deprecatedgenerateObject,generateImageinstead ofexperimental_generateImage,stopWhen: stepCountIs(2)instead ofmaxSteps). -
Added sections for tool calling, structured output, embeddings, image generation, and AI Search.
-
Uses
wrangler.jsoncformat for configuration examples.
-