Skip to content

Tool Calling using Vercel's AI SDK not working as intended #2864

@kldzj

Description

@kldzj

System Info

/info Output:

{
  "model_id": "casperhansen/llama-3.3-70b-instruct-awq",
  "model_sha": "64d255621f40b42adaf6d1f32a47e1d4534c0f14",
  "model_pipeline_tag": "text-generation",
  "max_concurrent_requests": 128,
  "max_best_of": 2,
  "max_stop_sequences": 4,
  "max_input_tokens": 8191,
  "max_total_tokens": 8192,
  "validation_workers": 2,
  "max_client_batch_size": 4,
  "router": "text-generation-router",
  "version": "3.0.2-dev0",
  "sha": "23bc38b10d06f8cc271d086c26270976faf67cc2",
  "docker_label": "sha-23bc38b"
}

Information

  • Docker
  • The CLI directly

Tasks

  • An officially supported command
  • My own modifications

Reproduction

  1. Install dependencies: npm i ai @ai-sdk/openai zod

Tool Calls

  1. Run the following script (e.g. node repro_generate.js):
const { createOpenAI } = require("@ai-sdk/openai");
const { generateObject } = require("ai");
const { z } = require("zod");

const openai = createOpenAI({
  baseURL: "http://localhost:8080/v1",
  apiKey: "-",
});

async function main() {
  const result = await generateObject({
    model: openai("casperhansen/llama-3.3-70b-instruct-awq"),
    system:
      "You are a summarization expert. Summarize the following text into a short title and a short description.",
    prompt:
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
    schema: z.object({
      title: z.string(),
      content: z.string(),
    }),
    temperature: 0.7,
    maxTokens: 200,
  });

  console.log(result.object);
}

main().catch(console.error);

This will result in an error from Vercels AI SDK:

Invalid JSON response
Error message: [
  {
    "code": "invalid_type",
    "expected": "string",
    "received": "object",
    "path": [
      "choices",
      0,
      "message",
      "tool_calls",
      0,
      "function",
      "arguments"
    ],
    "message": "Expected string, received object"
  }
]

For this issue, there's a simple workaround of rewriting the response and stringifying the function arguments:

// ...
const openai = createOpenAI({
  baseURL: "http://localhost:8080/v1",
  apiKey: "-",
  fetch: async (url, options) => {
    const response = await fetch(url, {
      ...options,
      body: JSON.stringify({
        ...JSON.parse(options.body),
      }),
    });

    const body = await response.json();
    const choices = body.choices.map((choice) => {
      if (choice.message.tool_calls) {
        choice.message.tool_calls = choice.message.tool_calls.map(
          (toolCall) => {
            return {
              ...toolCall,
              arguments: JSON.stringify(toolCall.function.arguments),
              function: {
                ...toolCall.function,
                arguments: JSON.stringify(toolCall.function.arguments),
              },
            };
          }
        );

        return {
          ...choice,
          message: {
            ...choice.message,
            tool_calls: choice.message.tool_calls,
          },
        };
      }
    });

    return new Response(JSON.stringify({ ...body, choices }), {
      status: response.status,
      headers: response.headers,
    });
  },
});
// ...

Tool Streaming

  1. Run the following script (e.g. node repro_stream.js):
const { createOpenAI } = require("@ai-sdk/openai");
const { generateObject, streamObject } = require("ai");
const { z } = require("zod");

const openai = createOpenAI({
  baseURL: "http://localhost:8080/v1",
  apiKey: "-",
});

async function main() {
  const result = await streamObject({
    model: openai("casperhansen/llama-3.3-70b-instruct-awq"),
    system:
      "You are a summarization expert. Summarize the following text into a short title and a short description.",
    prompt:
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
    schema: z.object({
      title: z.string(),
      content: z.string(),
    }),
    temperature: 0.7,
    maxTokens: 200,
  });

  for await (const partialObject of result.partialObjectStream) {
    console.log(partialObject);
  }
}

main().catch(console.error);

This will result in an error from Vercels AI SDK:

Type validation failed: Value: {"object":"chat.completion.chunk","id":"","created":1734971997,"model":"casperhansen/llama-3.3-70b-instruct-awq","system_fingerprint":"3.0.2-dev0-sha-23bc38b","choices":[{"index":0,"delta":{"role":"assistant","tool_calls":{"index":0,"id":"","type":"function","function":{"name":null,"arguments":"{\""}}},"logprobs":null,"finish_reason":null}],"usage":null}.
Error message: [
  {
    "code": "invalid_union",
    "unionErrors": [
      {
        "issues": [
          {
            "code": "invalid_type",
            "expected": "array",
            "received": "object",
            "path": [
              "choices",
              0,
              "delta",
              "tool_calls"
            ],
            "message": "Expected array, received object"
          }
        ],
        "name": "ZodError"
      },
      {
        "issues": [
          {
            "code": "invalid_type",
            "expected": "object",
            "received": "undefined",
            "path": [
              "error"
            ],
            "message": "Required"
          }
        ],
        "name": "ZodError"
      }
    ],
    "path": [],
    "message": "Invalid input"
  }
]

Edit: There's another one, the client sometimes sends response_format without value property:

requestBodyValues: {
  model: 'casperhansen/llama-3.3-70b-instruct-awq',
  logit_bias: undefined,
  logprobs: undefined,
  top_logprobs: undefined,
  user: undefined,
  parallel_tool_calls: undefined,
  max_tokens: 300,
  temperature: 0.7,
  top_p: undefined,
  frequency_penalty: undefined,
  presence_penalty: undefined,
  stop: undefined,
  seed: undefined,
  max_completion_tokens: undefined,
  store: undefined,
  metadata: undefined,
  response_format: { type: 'json_object' },
  messages: [ [Object], [Object] ]
}

which results in the following error: Failed to deserialize the JSON body into the target type: response_format: missing field value at line 1 column 126.

Expected behavior

Being able to call tools/generate objects using the OpenAI compatible clients like Vercels AI SDK.

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