Skip to content
This repository was archived by the owner on May 15, 2026. It is now read-only.

Commit 0546990

Browse files
committed
fix: switch HuggingFace to OpenAI-compatible SDK for tool support
- Replace @ai-sdk/huggingface with @ai-sdk/openai-compatible This fixes 'tool messages not supported' error (vercel/ai#10766) - Add createAiSdkToolStreamProcessor() for tool call deduplication Handles providers that emit tool-input-start/end but provide args only in the final tool-call event - Remove unused @ai-sdk/huggingface dependency - Update tests for new SDK
1 parent 33fd972 commit 0546990

3 files changed

Lines changed: 12 additions & 9 deletions

File tree

src/api/providers/__tests__/huggingface.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ vi.mock("ai", async (importOriginal) => {
1515
}
1616
})
1717

18-
vi.mock("@ai-sdk/huggingface", () => ({
19-
createHuggingFace: vi.fn(() => {
18+
vi.mock("@ai-sdk/openai-compatible", () => ({
19+
createOpenAICompatible: vi.fn(() => {
2020
// Return a function that returns a mock language model
2121
return vi.fn(() => ({
2222
modelId: "meta-llama/Llama-3.3-70B-Instruct",

src/api/providers/huggingface.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Anthropic } from "@anthropic-ai/sdk"
2-
import { createHuggingFace } from "@ai-sdk/huggingface"
2+
import { createOpenAICompatible } from "@ai-sdk/openai-compatible"
33
import { streamText, generateText, ToolSet } from "ai"
44

55
import type { ModelRecord, ModelInfo } from "@roo-code/types"
@@ -24,12 +24,13 @@ import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from ".
2424
const HUGGINGFACE_DEFAULT_TEMPERATURE = 0.7
2525

2626
/**
27-
* HuggingFace provider using the dedicated @ai-sdk/huggingface package.
28-
* Provides native support for various models on HuggingFace Hub via the Responses API.
27+
* HuggingFace provider using @ai-sdk/openai-compatible for OpenAI-compatible API.
28+
* Uses HuggingFace's OpenAI-compatible endpoint to enable tool message support.
29+
* @see https://github.com/vercel/ai/issues/10766 - Workaround for tool messages not supported in @ai-sdk/huggingface
2930
*/
3031
export class HuggingFaceHandler extends BaseProvider implements SingleCompletionHandler {
3132
protected options: ApiHandlerOptions
32-
protected provider: ReturnType<typeof createHuggingFace>
33+
protected provider: ReturnType<typeof createOpenAICompatible>
3334
private modelCache: ModelRecord | null = null
3435

3536
constructor(options: ApiHandlerOptions) {
@@ -40,8 +41,11 @@ export class HuggingFaceHandler extends BaseProvider implements SingleCompletion
4041
throw new Error("Hugging Face API key is required")
4142
}
4243

43-
// Create the HuggingFace provider using AI SDK
44-
this.provider = createHuggingFace({
44+
// Create an OpenAI-compatible provider pointing to HuggingFace's /v1 endpoint
45+
// This fixes "tool messages not supported" error - the HuggingFace SDK doesn't
46+
// properly handle function_call_output format, but OpenAI SDK does
47+
this.provider = createOpenAICompatible({
48+
name: "huggingface",
4549
baseURL: "https://router.huggingface.co/v1",
4650
apiKey: this.options.huggingFaceApiKey,
4751
headers: DEFAULT_HEADERS,

src/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,6 @@
454454
"@ai-sdk/deepseek": "^2.0.14",
455455
"@ai-sdk/fireworks": "^2.0.26",
456456
"@ai-sdk/groq": "^3.0.19",
457-
"@ai-sdk/huggingface": "^1.0.28",
458457
"@ai-sdk/mistral": "^3.0.0",
459458
"@ai-sdk/xai": "^3.0.46",
460459
"sambanova-ai-provider": "^1.2.2",

0 commit comments

Comments
 (0)