-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
drbh
wants to merge
3
commits into
main
Choose a base branch
from
improve-json-schema-field
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...models/__snapshots__/test_json_schema_constrain/test_json_schema_openai_style_format.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "choices": [ | ||
| { | ||
| "finish_reason": "stop", | ||
| "index": 0, | ||
| "logprobs": null, | ||
| "message": { | ||
| "content": "{\"status\":\".OK.\"}", | ||
| "role": "assistant" | ||
| } | ||
| } | ||
| ], | ||
| "created": 1750877897, | ||
| "id": "", | ||
| "model": "google/gemma-3-4b-it", | ||
| "object": "chat.completion", | ||
| "system_fingerprint": "3.3.4-dev0-native", | ||
| "usage": { | ||
| "completion_tokens": 8, | ||
| "prompt_tokens": 36, | ||
| "total_tokens": 44 | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
...tests/models/__snapshots__/test_json_schema_constrain/test_json_schema_simple_status.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "choices": [ | ||
| { | ||
| "finish_reason": "stop", | ||
| "index": 0, | ||
| "logprobs": null, | ||
| "message": { | ||
| "content": "{\"status\":\".OK.\"}", | ||
| "role": "assistant" | ||
| } | ||
| } | ||
| ], | ||
| "created": 1750877897, | ||
| "id": "", | ||
| "model": "google/gemma-3-4b-it", | ||
| "object": "chat.completion", | ||
| "system_fingerprint": "3.3.4-dev0-native", | ||
| "usage": { | ||
| "completion_tokens": 8, | ||
| "prompt_tokens": 36, | ||
| "total_tokens": 44 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -224,7 +224,7 @@ impl HubProcessorConfig { | |
|
|
||
| #[derive(Clone, Debug, Deserialize, ToSchema, Serialize)] | ||
| #[cfg_attr(test, derive(PartialEq))] | ||
| struct JsonSchemaConfig { | ||
| pub struct JsonSchemaConfig { | ||
| /// Optional name identifier for the schema | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| name: Option<String>, | ||
|
|
@@ -235,7 +235,7 @@ struct JsonSchemaConfig { | |
|
|
||
| #[derive(Clone, Debug, Deserialize, ToSchema, Serialize)] | ||
| #[cfg_attr(test, derive(PartialEq))] | ||
| #[serde(tag = "type", content = "value")] | ||
| #[serde(tag = "type")] | ||
| pub(crate) enum GrammarType { | ||
| /// A string that represents a [JSON Schema](https://json-schema.org/). | ||
| /// | ||
|
|
@@ -244,17 +244,53 @@ pub(crate) enum GrammarType { | |
| #[serde(rename = "json")] | ||
| #[serde(alias = "json_object")] | ||
| #[schema(example = json ! ({"properties": {"location":{"type": "string"}}}))] | ||
| Json(serde_json::Value), | ||
| Json { value: serde_json::Value }, | ||
|
|
||
| #[serde(rename = "regex")] | ||
| Regex(String), | ||
| Regex { value: String }, | ||
|
|
||
| /// A JSON Schema specification with additional metadata. | ||
| /// | ||
| /// Includes an optional name for the schema, an optional strict flag, and the required schema definition. | ||
| #[serde(rename = "json_schema")] | ||
| #[schema(example = json ! ({"schema": {"properties": {"name": {"type": "string"}, "age": {"type": "integer"}}}, "name": "person_info", "strict": true}))] | ||
| JsonSchema(JsonSchemaConfig), | ||
| JsonSchema(JsonSchemaFormat), | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, Deserialize, ToSchema, Serialize)] | ||
| #[cfg_attr(test, derive(PartialEq))] | ||
| #[serde(untagged)] | ||
| pub enum JsonSchemaFormat { | ||
| JsonSchema { json_schema: JsonSchemaOrConfig }, | ||
| Value { value: JsonSchemaOrConfig }, | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, Deserialize, ToSchema, Serialize)] | ||
| #[cfg_attr(test, derive(PartialEq))] | ||
| #[serde(untagged)] | ||
| pub enum JsonSchemaOrConfig { | ||
| Config(JsonSchemaConfig), | ||
| Value(serde_json::Value), | ||
|
||
| } | ||
|
|
||
| impl JsonSchemaOrConfig { | ||
| pub fn schema_value(&self) -> &serde_json::Value { | ||
| match self { | ||
| JsonSchemaOrConfig::Config(config) => &config.schema, | ||
| JsonSchemaOrConfig::Value(value) => value, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl JsonSchemaFormat { | ||
| pub fn schema_value(&self) -> &serde_json::Value { | ||
| let config = match self { | ||
| Self::JsonSchema { json_schema } | Self::Value { value: json_schema } => json_schema, | ||
| }; | ||
| match config { | ||
| JsonSchemaOrConfig::Config(config) => &config.schema, | ||
| JsonSchemaOrConfig::Value(value) => value, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, Serialize, ToSchema)] | ||
|
|
@@ -984,7 +1020,9 @@ impl ChatRequest { | |
| if let Some(tools) = tools { | ||
| match ToolGrammar::apply(tools, tool_choice)? { | ||
| Some((updated_tools, tool_schema)) => { | ||
| let grammar = GrammarType::Json(serde_json::json!(tool_schema)); | ||
| let grammar = GrammarType::Json { | ||
| value: serde_json::json!(tool_schema), | ||
| }; | ||
| let inputs: String = infer.apply_chat_template( | ||
| messages, | ||
| Some((updated_tools, tool_prompt)), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.