Skip to content

feat: allow json_schema in response format and add test #3276

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

drbh
Copy link
Collaborator

@drbh drbh commented Jun 25, 2025

This PR improve the response_format.type = "json_schema" to allow the schema to be specified with the key value or json_schema, prior to these changes only the value key would be allowed.

These changes enable the two following request to succeed:

import requests
import json

# API endpoint
url = "http://localhost:3000/v1/chat/completions"

# Your payload
payload = {
   "temperature": 0,
   "response_format": {
       "value": {
           "name": "test",
           "strict": True,
           "schema": {
               "type": "object",
               "properties": {"status": {"type": "string"}},
               "required": ["status"],
               "additionalProperties": False,
           },
       },
       "type": "json_schema",
   },
   "messages": [
       {
           "role": "system",
           "content": "You are a helpful assistant. You answer with a JSON output with a status string containing the value \u0027OK\u0027",
       },
       {"role": "user", "content": "Please tell me OK"},
   ],
   "model": "tgi",
   "max_completion_tokens": 8192,
}

payload1 = {
  "temperature": 0,
  "response_format": {
      "json_schema": {
          "name": "test",
          "strict": True,
          "schema": {
              "type": "object",
              "properties": {"status": {"type": "string"}},
              "required": ["status"],
              "additionalProperties": False,
          },
      },
      "type": "json_schema",
  },
  "messages": [
      {
          "role": "system",
          "content": "You are a helpful assistant. You answer with a JSON output with a status string containing the value \u0027OK\u0027",
      },
      {"role": "user", "content": "Please tell me OK"},
  ],
  "model": "tgi",
  "max_completion_tokens": 8192,
}

# Send request
for payload in [payload, payload1]:
  response = requests.post(url, json=payload)

  # Print response
  print("Status Code:", response.status_code)
  print("Response JSON:", json.dumps(response.json(), indent=4))

Copy link
Collaborator

@Narsil Narsil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you find a way to remove ALL serde(untagged), and have simpler hierarchy ?

I feel like unit tests with valid schemas and how we expect to parse them should exist in here.

Comment on lines +263 to +264
JsonSchema { json_schema: JsonSchemaOrConfig },
Value { value: JsonSchemaOrConfig },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like we just want serde(alias="value") if I read correctly.

Comment on lines +268 to +272
#[cfg_attr(test, derive(PartialEq))]
#[serde(untagged)]
pub enum JsonSchemaOrConfig {
Config(JsonSchemaConfig),
Value(serde_json::Value),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is extremely fishy. untagged + recursive descent seems like a great way to shoot ourselves.

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

Successfully merging this pull request may close these issues.

2 participants