@@ -495,7 +495,10 @@ class EngineImpl : public Engine {
495
495
496
496
int n = request->generation_cfg ->n ;
497
497
int rng_seed = request->generation_cfg ->seed ;
498
- auto compiled_grammar = GetGrammarFromResponseFormat (request->generation_cfg ->response_format );
498
+ auto compiled_grammar =
499
+ GetGrammarFromResponseFormat (request->generation_cfg ->response_format ,
500
+ request->generation_cfg ->debug_config .disable_grammar_cache ,
501
+ request->generation_cfg ->debug_config .compact_json_output );
499
502
500
503
std::vector<RequestStateEntry> rsentries;
501
504
// Create the request state entry for the input.
@@ -814,14 +817,27 @@ class EngineImpl : public Engine {
814
817
/* ! \brief Create a grammar init context according to the response format. If the response format
815
818
* is not JSON, return std::nullopt. */
816
819
std::optional<xgrammar::CompiledGrammar> GetGrammarFromResponseFormat (
817
- const ResponseFormat& response_format) {
820
+ const ResponseFormat& response_format, bool disable_grammar_cache, bool compact_json_output ) {
818
821
if (response_format.type != " json_object" ) {
819
822
return std::nullopt ;
820
823
} else if (!response_format.schema ) {
821
824
return cached_grammar_compiler_.GetCompiledGrammarForJSON ();
822
825
} else {
823
- return cached_grammar_compiler_.GetCompiledGrammarForJSONSchema (
824
- response_format.schema .value ());
826
+ std::optional<int > indent = std::nullopt ;
827
+ std::optional<std::pair<std::string, std::string>> separators = std::nullopt ;
828
+ if (!compact_json_output) {
829
+ std::optional<int > indent = 2 ;
830
+ std::optional<std::pair<std::string, std::string>> separators = std::make_pair (" : " , " ," );
831
+ }
832
+ if (disable_grammar_cache) {
833
+ return xgrammar::CompiledGrammar (
834
+ xgrammar::BuiltinGrammar::JSONSchema (response_format.schema .value (), indent, separators,
835
+ true ),
836
+ token_table_);
837
+ } else {
838
+ return cached_grammar_compiler_.GetCompiledGrammarForJSONSchema (
839
+ response_format.schema .value (), indent, separators, true );
840
+ }
825
841
}
826
842
}
827
843
0 commit comments