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

OpenAI.instance.chat.create() accepts the wrong type for responseFormat #204

Closed
seung-cha opened this issue Jan 5, 2025 · 2 comments
Closed

Comments

@seung-cha
Copy link

seung-cha commented Jan 5, 2025

current definiiton (5.1.0) accepts Map<String, String> for responseFormat. This is incompatible when you are using json_schema.

image

MRE:

final responseFormatDef = {
      "type": "json_schema",
      "json_schema": jsonEncode({
        "name": "story_generation",
        "schema": {
          "type": "object",
          "properties": {
            "title": {
              "type": "string",
              "description": "The title of the story."
            },
            "genre": {
              "type": "string",
              "description":
                  "The genre of the story, as provided by the user. If user asks for any, make one yourself."
            },
            "language": {
              "type": "string",
              "description":
                  "The language in which the story is written, as requested by the user."
            },
            "content": {
              "type": "string",
              "description": "The main body of the story."
            }
          },
          "required": ["title", "genre", "language", "content"],
          "additionalProperties": false
        },
        "strict": true
      })
    };

    OpenAIChatCompletionModel completion = await OpenAI.instance.chat.create(
        model: 'gpt-4o',
        responseFormat: responseFormatDef,
        messages: [sysMsg, usrMsg]);

Here, I used jsonEncode to convert the json_schema field into a string. This consequently causes .create() to fail.
Once I change the type definition to Map<String, dynamic> and get rid of jsonEncode, it works fine (changing value type to dynamic makes it more versatile too).

@seung-cha
Copy link
Author

PR opened: #205

@seung-cha
Copy link
Author

dup: #193

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant