Skip to content

Potential Bug with toolChoice forced function mode #443

@KatjaFraedrich

Description

@KatjaFraedrich

Bug Report

Overview

When using forced function calling (tool_choice with "type": "function") in the Realtime Sessions API via the Unity OpenAI package, the request fails with a 400 error:

Missing required parameter: 'tool_choice.name'.

The issue appears to be caused by the Unity package generating an incorrect JSON structure for tool_choice. The package sends:

"tool_choice": {
  "type": "function",
  "function": {
    "name": "MyFunctionName"
  }
}

However, the Realtime Sessions API expects:

"tool_choice": {
  "type": "function",
  "name": "MyFunctionName"
}

This mismatch causes the server to reject the request.


To Reproduce

Steps to reproduce the behavior:

  1. Create a Realtime session using CreateSessionAsync.
  2. Register one or more tools (functions).
  3. Configure tool_choice to force a specific function.
  4. Start the session.
  5. Observe the 400 Bad Request response from the server.

Example configuration sent by the package:

"tool_choice": {
  "type": "function",
  "function": {
    "name": "ExecuteAnimation_abc123"
  }
}

Expected behavior

The session should be created successfully when forcing a valid registered tool via tool_choice.

The request body should match the Realtime Sessions API specification:

"tool_choice": {
  "type": "function",
  "name": "ExecuteAnimation_abc123"
}

Screenshots

N/A (server error response included below)

Server response:

{
  "error": {
    "message": "Missing required parameter: 'tool_choice.name'.",
    "type": "invalid_request_error",
    "param": "tool_choice.name",
    "code": "missing_required_parameter"
  }
}

Additional context

The issue originates in the Unity package's ToolExtensions.ProcessTools<T> method:

activeTool = new
{
    type = "function",
    function = new { name = tool.Function.Name }
};

This produces a nested "function": { "name": ... } structure, which does not conform to the Realtime Sessions API specification.

According to the official documentation:
https://developers.openai.com/api/docs/guides/realtime-conversations

When type is "function", the name field must be provided directly under tool_choice.

Suggested Fix

Replace:

activeTool = new
{
    type = "function",
    function = new { name = tool.Function.Name }
};

With:

activeTool = new
{
    type = "function",
    name = tool.Function.Name
};

This aligns the generated request with the API specification and resolves the 400 error.

Metadata

Metadata

Labels

bugSomething isn't workingneeds-validationThis doesn't seem right

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions