Skip to content

OpenAI-compatible providers fail with tools - missing 'type: object' in JSON Schema #7924

@hughlv

Description

@hughlv

Bug Report: OpenAI-compatible providers fail with tools - missing "type": "object" in JSON Schema

Description

When using tools with OpenAI-compatible providers that strictly validate JSON schemas (like DeepSeek), all tool calls fail with the error:

Invalid schema for function 'functionName': schema must be a JSON Schema of 'type: "object"', got 'type: null'.

Root Cause

The bug is in the OpenAI-compatible provider used by DeepSeek. When converting tool schemas, the AI SDK creates JSON schemas without the required "type": "object" field.

Location in source code:

  1. @ai-sdk/openai-compatible/dist/index.js line 239: Directly passes tool.inputSchema without validation
  2. @ai-sdk/provider-utils/dist/index.js lines 1031-1033: Creates schema without type field:
jsonSchema({
  properties: {},
  additionalProperties: false
})

Reproduction

import { streamText } from 'ai';
import { deepseek } from '@ai-sdk/deepseek';
import { z } from 'zod';

const result = await streamText({
  model: deepseek('deepseek-chat'),
  messages: [{ role: 'user', content: 'What is the weather?' }],
  tools: {
    getWeather: {
      description: 'Get weather',
      parameters: z.object({
        location: z.string(),
      }),
      execute: async () => ({ weather: 'sunny' }),
    },
  },
});

What AI SDK sends (incorrect):

{
  "type": "function",
  "function": {
    "name": "getWeather",
    "description": "Get weather",
    "parameters": {
      "properties": {},
      "additionalProperties": false
    }
  }
}

What DeepSeek expects (correct):

{
  "type": "function",
  "function": {
    "name": "getWeather",
    "description": "Get weather",
    "parameters": {
      "type": "object",  // <-- This is missing!
      "properties": {
        "location": {
          "type": "string"
        }
      },
      "required": ["location"]
    }
  }
}

Verification

Direct API call to DeepSeek works fine with proper schema:

curl -X POST https://api.deepseek.com/v1/chat/completions \
  -H "Authorization: Bearer $DEEPSEEK_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-chat",
    "messages": [{"role": "user", "content": "What is the weather?"}],
    "tools": [{
      "type": "function",
      "function": {
        "name": "getWeather",
        "parameters": {
          "type": "object",
          "properties": {"location": {"type": "string"}}
        }
      }
    }]
  }'

Proposed Fix

In @ai-sdk/provider-utils, the jsonSchema function should include "type": "object":

function jsonSchema(jsonSchema2, { validate } = {}) {
  // Ensure type: 'object' is set for object schemas
  if (jsonSchema2.properties && !jsonSchema2.type) {
    jsonSchema2.type = 'object';
  }
  // ... rest of function
}

Or in the asSchema function:

function asSchema(schema) {
  return schema == null ? jsonSchema({
    type: 'object',  // Add this line
    properties: {},
    additionalProperties: false
  }) : isSchema(schema) ? schema : zodSchema(schema);
}

Environment

  • @ai-sdk/deepseek: 1.0.3
  • @ai-sdk/openai-compatible: 1.0.3
  • @ai-sdk/provider-utils: 3.0.1
  • ai: 5.0.8

Impact

This bug prevents all tool usage with DeepSeek through the AI SDK, making it impossible to use function calling with this provider.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions