microsoft / microsoft/semantic-kernel

.Net: Bug: .NET - Structured Output ResponseFormat schema incorrect for arrays of repeated types --- Invalid schema for response_format - reference can only point to definitions defined at the top level of the schema.

Open
#10,142 9 comments 3 reactions 1 assignee View on GitHub

@SergeyMenshykh is already working on this.

Since Jan 13, 2025.

.NET bug
Dominant language
C#
Stars
28.6k
Forks
4.8k
Avg merge
14h 13m
Merged PRs (30d)
18

Description

Describe the bug
When using ResponseFormat = typeof(MyClass), the schema is incorrectly generated when two array properties use the same object type, leading to the following error:

{
  "error": {
    "message": "Invalid schema for response_format 'MyClass': In context=('properties', 'PropertyB', 'items'), reference can only point to definitions defined at the top level of the schema.",
    "type": "invalid_request_error",
    "param": "response_format",
    "code": null
  }
}

To Reproduce
Steps to reproduce the behavior:

See this MRE:

Program.cs

OpenAIPromptExecutionSettings executionSettings = new() { ResponseFormat = typeof(MyClass) };
 KernelArguments kernelArgs = new(executionSettings);
await kernel.InvokeAsync("PluginName", "FunctionName", kernelArgs);

MyClass.cs

public class MyClass
{
  public SubClass[] PropertyA { get; set; }
  public SubClass[] PropertyB { get; set; }
}

SubClass.cs

public class SubClass
{
  public string Property1 { get; set; }
  public int Property2 { get; set; }
}

Invalid generated schema:

{
  "response_format": {
    "json_schema": {
      "name": "MyClass",
      "schema": {
        "type": "object",
        "properties": {
          "PropertyA": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "Property1": { "type": "string" },
                "Property2": { "type": "integer" }
              },
              "additionalProperties": false,
              "required": ["Property1", "Property2"]
            }
          },
          "PropertyB": {
            "type": "array",
            "items": { "$ref": "#/properties/PropertyA/items" }
          }
        },
        "additionalProperties": false,
        "required": ["PropertyA", "PropertyB"]
      },
      "strict": true
    },
    "type": "json_schema"
  }
}

Expected valid schema

{
  "response_format": {
    "json_schema": {
      "name": "MyClass",
      "schema": {
        "type": "object",
        "properties": {
          "PropertyA": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "Property1": { "type": "string" },
                "Property2": { "type": "integer" }
              },
              "additionalProperties": false,
              "required": ["Property1", "Property2"]
            }
          },
          "PropertyB": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "Property1": { "type": "string" },
                "Property2": { "type": "integer" }
              },
              "additionalProperties": false,
              "required": ["Property1", "Property2"]
            }
          }
        },
        "additionalProperties": false,
        "required": ["PropertyA", "PropertyB"]
      },
      "strict": true
    },
    "type": "json_schema"
  }
}

Expected behavior
The generated schema is valid, and PropertyB doesn't use a reference to PropertyA

Platform

  • Language: C#
  • Source: Semantic Kernel version 1.33.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.