Skip to content

[M.E.AI] Tool call format problems #6483

@ForkBug

Description

@ForkBug

Description

When starting a tool call request. there are some issue in the request json.
The following AIFunctionOption is used:

                var fo = new AIFunctionFactoryOptions()
                {
                    Name = "get_current_weather",
                    Description = "Get the current weather for a given location",
                    MarshalResult = async (o, _, _) => { return o; },
                    SerializerOptions = new System.Text.Json.JsonSerializerOptions(System.Text.Json.JsonSerializerDefaults.Web)
                    {
                        WriteIndented = false,
                        TypeInfoResolver = new System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver()
                    },
                    JsonSchemaCreateOptions = new AIJsonSchemaCreateOptions()
                    {
                        TransformOptions = new AIJsonSchemaTransformOptions()
                        {
                            DisallowAdditionalProperties = true,
                        }
                    }
                };

But the following request json is post:

HTTP Request: 49fa56aa-415c-4983-84d4-6ab8059e0372
Method: POST
Uri: https://api.openai.com/chat/completions
Headers:
  Accept: application/json
  User-Agent: OpenAI/2.2.0-beta.4, (.NET 9.0.5; Microsoft Windows 10.0.19045)
  Authorization: Bearer sk-xxxxxxxxxxxxxxxxxxxxxxx
  Content-Type: application/json
Content:
{"tools":[{"function":{"description":"Get the current weather for a given location","name":"get_current_weather","parameters":{
  "type": "object",
  "required": [
    "location",
    "unit"
  ],
  "properties": {
    "location": {
      "type": "string"
    },
    "unit": {
      "type": "string"
    }
  },
  "additionalProperties": false
},"type":"function"}],"messages":[{"role":"user","content":"Please say: Hello, AI World! This is a test."}],"model":"gpt-4o","stream":true,"stream_options":{"include_usage":true},"tool_choice":"auto"}

Beautified version:

{
    "tools": [
        {
            "function": {
                "description": "Get the current weather for a given location",
                "name": "get_current_weather",
                "parameters": {
                    "type": "object",
                    "required": [
                        "location",
                        "unit"
                    ],
                    "properties": {
                        "location": {
                            "type": "string"
                        },
                        "unit": {
                            "type": "string"
                        }
                    },
                    "additionalProperties": false
                },
                "type": "function"
            }
        ],
        "messages": [
            {
                "role": "user",
                "content": "Please say: Hello, AI World! This is a test."
            }
        ],
        "model": "gpt-4o",
        "stream": true,
        "stream_options": {
            "include_usage": true
        },
        "tool_choice": "auto"
    }

We can see the following issues:

  1. There is still "additionalProperties" json node despite of "DisallowAdditionalProperties = true" . Is a wrong setting used?
  2. We got json indent even "WriteIndented = false" is set.
  3. Unmatched json braces. This issue impact a lot. It may cause problems when some providers are using tool calls. It may caused by Malformed tool call request format openai/openai-dotnet#440 But I'm not sure about this.

Reproduction Steps

This is a minimal reproducing project. Just 2 cs files. ConsoleApp3.zip

  1. Download the attachment and run it with
    dotnet run -- sk-xxxxxxxxxxxxxxxxxxxxxxx gpt-4o

  2. Look into "logs" folder

Expected behavior

  1. No "additionalProperties" json node when "DisallowAdditionalProperties = true"
  2. No json indented when "WriteIndented = false"
  3. Properly tool calling

Actual behavior

  1. There is still "additionalProperties" json node despite of "DisallowAdditionalProperties = true" . Is a wrong setting used?
  2. We got json indented even "WriteIndented = false" is set.
  3. Unmatched json braces. This issue impact a lot.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-aiMicrosoft.Extensions.AI libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions