Skip to content

DeepSeek API Compatibility Issue: JSON Format Requirement and Schema Mismatch #1204

@MingGuangShao

Description

@MingGuangShao

Problem Description

When using DeepSeek API (deepseek/deepseek-chat) with Stagehand, there are two related issues:

  1. Missing "json" keyword error: When using DeepSeek API, if the prompt doesn't explicitly contain the word "json", the API returns an error: Prompt must contain the word 'json' in some form to use 'response_format' of type 'json_object'

  2. Schema mismatch error: When adding "json" to the prompt to satisfy DeepSeek's requirement, the model returns a JSON structure that doesn't match Stagehand's expected schema. DeepSeek returns a format like:

    {
      "action": "fill",
      "element": {"id": "...", "role": "...", "name": "..."},
      "argument": "Cota",
      "twoStep": false
    }

    But Stagehand expects:

    {
      "elementId": "string (required)",
      "description": "string (required)",
      "method": "string (required)",
      "arguments": ["array (required)"]
    }

This creates a catch-22 situation: without "json" in the prompt, DeepSeek API rejects the request; with "json" in the prompt, the response format doesn't match Stagehand's schema.

Environment

  • Stagehand version: @browserbasehq/stagehand@^3.0.0
  • Node.js version: v22.2.0
  • DeepSeek API endpoint: https://api.deepseek.com/v1
  • Model: deepseek/deepseek-chat

Code to Reproduce

import "dotenv/config";
import { Stagehand } from "@browserbasehq/stagehand";
import { z } from "zod/v3";

async function main() {
  const stagehand = new Stagehand({
    env: "LOCAL",
    model: "deepseek/deepseek-chat"
  });

  await stagehand.init();
  const page = stagehand.context.pages()[0];

  await page.goto("https://www.baidu.com");

  await stagehand.act("fill the search input with the text 'hello'");
  await stagehand.act("click the search button");

  // Extract structured data
  const title = await stagehand.extract("extract the page title", z.string());

  console.log(title);
  await stagehand.close();
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});

Error Messages

Error 1: Missing "json" keyword

APICallError [AI_APICallError]: Prompt must contain the word 'json' in some form to use 'response_format' of type 'json_object'.
    at ...
  url: 'https://api.deepseek.com/v1/chat/completions',
  requestBodyValues: {
    model: 'deepseek-chat',
    response_format: { type: 'json_object' },
    messages: [ [Object], [Object] ],
    ...
  },
  statusCode: 400,
  responseBody: `{"error":{"message":"Prompt must contain the word 'json' in some form to use 'response_format' of type 'json_object'.","type":"invalid_request_error","param":null,"code":"invalid_request_error"}}`

Error 2: Schema validation failure (when "json" is added to prompt)

NoObjectGeneratedError [AI_NoObjectGeneratedError]: No object generated: response did not match schema.
  cause: _TypeValidationError [AI_TypeValidationError]: Type validation failed
  Error message: [
    {
      "code": "invalid_type",
      "expected": "string",
      "received": "undefined",
      "path": ["elementId"],
      "message": "Required"
    },
    {
      "code": "invalid_type",
      "expected": "string",
      "received": "undefined",
      "path": ["description"],
      "message": "Required"
    },
    {
      "code": "invalid_type",
      "expected": "string",
      "received": "undefined",
      "path": ["method"],
      "message": "Required"
    },
    {
      "code": "invalid_type",
      "expected": "array",
      "received": "undefined",
      "path": ["arguments"],
      "message": "Required"
    }
  ]
  value: {
    action: 'fill',
    element: { id: '...', role: 'textbox', name: '...' },
    argument: 'Cota',
    twoStep: false
  }

Questions

  1. Is DeepSeek officially supported by Stagehand? If not, what would be needed to add support?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions