Skip to content

Commit 4d20853

Browse files
committed
feat(vertexai): support meta JSON object mode
1 parent 75247bb commit 4d20853

2 files changed

Lines changed: 43 additions & 12 deletions

File tree

packages/genkit_vertexai/lib/src/meta_model.dart

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
165175
ModelResponse fromMetaChatCompletionResponse(Map<String, dynamic> response) {
166176
final choices = response['choices'] as List?;
167177
if (choices == null || choices.isEmpty) {

packages/genkit_vertexai/test/meta_test.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,27 @@ void main() {
124124
);
125125
});
126126

127+
test('uses JSON object response format without schema', () {
128+
final request = ModelRequest(
129+
messages: [
130+
Message(
131+
role: Role.user,
132+
content: [TextPart(text: 'return json')],
133+
),
134+
],
135+
output: OutputConfig(format: 'json'),
136+
);
137+
138+
final body = toMetaChatCompletionRequest(
139+
request,
140+
'meta/llama-4-maverick-17b-128e-instruct-maas',
141+
VertexAiMetaOptions(),
142+
stream: false,
143+
);
144+
145+
expect(body['response_format'], {'type': 'json_object'});
146+
});
147+
127148
test('aggregates streamed tool call chunks', () {
128149
final response = fromMetaChatCompletionChunks([
129150
{

0 commit comments

Comments
 (0)