Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Valid OpenAI API Schema is not possible with JSONSchemaDefine because JSONSchemaType doesn't allow union types #141

Open
TheLexoPlexx opened this issue Jan 8, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@TheLexoPlexx
Copy link

Describe the bug

So, this is valid OpenAI-Schema, taken from the docs:

{
  "name": "get_weather",
  "description": "Fetches the weather in the given location",
  "strict": true,
  "parameters": {
      "type": "object",
      "properties": {
          "location": {
              "type": "string",
              "description": "The location to get the weather for"
          },
          "unit": {
              "type": ["string", "null"],
              "description": "The unit to return the temperature in",
              "enum": ["F", "C"]
          }
      },
      "additionalProperties": false,
      "required": [
          "location", "unit"
      ]
  }
}

Source: https://platform.openai.com/docs/guides/structured-outputs/supported-schemas#supported-schemas

Although all fields must be required (and the model will return a value for each parameter), it is possible to emulate an optional parameter by using a union type with null.

But it is disallowed by: JSONSchemaType

Is this intentional? If not, I can create a PR if you like, but it would be a tiny fix afaik.

To Reproduce

  1. Run:
use std::process::exit;

use openai_api_rs::v1::types::Function;

fn main() {
    let sample = r#"
{
    "name": "get_weather",
    "description": "Fetches the weather in the given location",
    "strict": true,
    "parameters": {
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "The location to get the weather for"
            },
            "unit": {
                "type": ["string", "null"],
                "description": "The unit to return the temperature in",
                "enum": ["F", "C"]
            }
        },
        "additionalProperties": false,
        "required": [
            "location", "unit"
        ]
    }
}
    "#;

    let parser: Function = serde_json::from_str(&sample).expect("Error");

    dbg!(parser);
}

(I don't know if there's a rust playground that allows crates to be used).

Code snippets

No response

OS

Windows 11 Pro 32H2 22631.4391

Rust version

rustc 1.83.0 (90b35a623 2024-11-26)

Library version

5.2.4

@TheLexoPlexx TheLexoPlexx added the bug Something isn't working label Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant