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

Incompatibility between OpenAI and HF's Chat Completion response_format #932

Open
sadra-barikbin opened this issue Sep 27, 2024 · 3 comments

Comments

@sadra-barikbin
Copy link

Hi there!

OpenAI and HF api seem to have diverged on chat_completion response_format. That of the OpenAI has a type attribute which takes one of three values text, json_object (free-form JSON generation which isn't in TGI yet) and json_schema, and for the last one it has another json_schema attribute which itself contains name, schema, description and strict attributes.

On the other hand, HF's response_format: ChatCompletionInputGrammarType contains type and value attributes.

This might break the OpenAI api usability on HF models. Could we have compatibility between these two on this matter? Or does it need probably breaking changes in TGI and huggingface_hub?

@Wauplin

@Wauplin
Copy link
Contributor

Wauplin commented Sep 27, 2024

Cc @drbh @Narsil @OlivierDehaene on the TGI side

@Cristy94
Copy link

Cristy94 commented Jan 7, 2025

I have the same issue, I could easily add the Gemini and ChatGPT API, but with the Huggingface API I have to change my code and define JSON schemas, because both Gemini and GPT-4 work without a schema defined too.

Can we get JSON responses without passing any value for the response_format ?

Now I do this, I don't need to pass any schema and it works well (for Gemini/ChatGPT):
response_format: json ? { type: "json_object" } : undefined

@Cristy94
Copy link

Cristy94 commented Jan 7, 2025

If you try to simply use the OpenAI format, you get this error:

Failed to deserialize the JSON body into the target type: response_format.type: unknown variant `json_schema`, expected one of `json`, `json_object`, `regex`

If you try to pass the required HF format to openai-node:

   return {
                type: "json",
                value: schema,
        }

The request will fail with the error:

UnprocessableEntityError: 422 "Input validation error: cannot compile regex from schema: 'type' must be a string"

So, I assume the schema is still not properly sent.

Any way to use HF chat completion with response_format json or json_schema through openai-node?

EDIT: It seems to work, the problem was with the JSON schema.

This works in OpenAI
description: { "type": ["string", "null"] },

I had to change it to this for HF:

description: { anyOf: [{ type: "string" }, { type: "null" }] },

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

3 participants