@@ -124,9 +124,7 @@ Map<String, dynamic> toMetaChatCompletionRequest(
124124 VertexAiMetaOptions options, {
125125 required bool stream,
126126}) {
127- final isJsonMode =
128- req.output? .format == 'json' ||
129- req.output? .contentType == 'application/json' ;
127+ final responseFormat = _toMetaResponseFormat (req.output);
130128 return {
131129 'model' : options.version ?? modelName,
132130 'messages' : _toMetaMessages (req.messages),
@@ -141,15 +139,7 @@ Map<String, dynamic> toMetaChatCompletionRequest(
141139 'frequency_penalty' : ? options.frequencyPenalty,
142140 'seed' : ? options.seed,
143141 'user' : ? options.user,
144- if (isJsonMode && req.output? .schema != null )
145- 'response_format' : {
146- 'type' : 'json_schema' ,
147- 'json_schema' : {
148- 'name' : 'output' ,
149- 'schema' : {...req.output! .schema! , 'additionalProperties' : false },
150- 'strict' : true ,
151- },
152- },
142+ if (responseFormat != null ) 'response_format' : responseFormat,
153143 if (options.llamaGuard != null )
154144 'extra_body' : {
155145 'google' : {
@@ -162,6 +152,26 @@ Map<String, dynamic> toMetaChatCompletionRequest(
162152 };
163153}
164154
155+ Map <String , dynamic >? _toMetaResponseFormat (OutputConfig ? output) {
156+ final isJsonMode =
157+ output? .format == 'json' || output? .contentType == 'application/json' ;
158+ if (! isJsonMode) return null ;
159+
160+ final schema = output? .schema;
161+ if (schema == null ) {
162+ return {'type' : 'json_object' };
163+ }
164+
165+ return {
166+ 'type' : 'json_schema' ,
167+ 'json_schema' : {
168+ 'name' : 'output' ,
169+ 'schema' : {...schema, 'additionalProperties' : false },
170+ 'strict' : true ,
171+ },
172+ };
173+ }
174+
165175ModelResponse fromMetaChatCompletionResponse (Map <String , dynamic > response) {
166176 final choices = response['choices' ] as List ? ;
167177 if (choices == null || choices.isEmpty) {
0 commit comments