Skip to content

Commit 5199edb

Browse files
authored
feat(dev-api): add inlineData support to Developer API (#17600)
The Developer API was missing support for parts, which was already implemented in the Vertex AI API. This change copies the parsing logic from the Vertex AI function to the function in the Developer API. An existing test file, , has been updated to include a test case for parsing parts, ensuring the functionality is working as expected.
1 parent b03381a commit 5199edb

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

packages/firebase_ai/firebase_ai/lib/src/developer/api.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import 'dart:convert';
16+
1517
import '../api.dart'
1618
show
1719
BlockReason,
@@ -31,7 +33,8 @@ import '../api.dart'
3133
SerializationStrategy,
3234
UsageMetadata,
3335
createUsageMetadata;
34-
import '../content.dart' show Content, FunctionCall, Part, TextPart;
36+
import '../content.dart'
37+
show Content, FunctionCall, InlineDataPart, Part, TextPart;
3538
import '../error.dart';
3639
import '../tool.dart' show Tool, ToolConfig;
3740

@@ -322,8 +325,8 @@ Part _parsePart(Object? jsonObject) {
322325
'functionResponse': {'name': String _, 'response': Map<String, Object?> _}
323326
} =>
324327
throw UnimplementedError('FunctionResponse part not yet supported'),
325-
{'inlineData': {'mimeType': String _, 'data': String _}} =>
326-
throw UnimplementedError('inlineData content part not yet supported'),
328+
{'inlineData': {'mimeType': String mimeType, 'data': String bytes}} =>
329+
InlineDataPart(mimeType, base64Decode(bytes)),
327330
_ => throw unhandledFormat('Part', jsonObject),
328331
};
329332
}

packages/firebase_ai/firebase_ai/test/developer_api_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
import 'dart:convert';
15+
import 'dart:typed_data';
16+
17+
import 'package:firebase_ai/src/content.dart';
1418
import 'package:firebase_ai/src/developer/api.dart';
1519
import 'package:flutter_test/flutter_test.dart';
1620

@@ -121,6 +125,34 @@ void main() {
121125
DeveloperSerialization().parseGenerateContentResponse(jsonResponse);
122126
expect(response.usageMetadata, isNull);
123127
});
128+
129+
test('parses inlineData part correctly', () {
130+
final inlineData = Uint8List.fromList([1, 2, 3, 4]);
131+
final jsonResponse = {
132+
'candidates': [
133+
{
134+
'content': {
135+
'role': 'model',
136+
'parts': [
137+
{
138+
'inlineData': {
139+
'mimeType': 'application/octet-stream',
140+
'data': base64Encode(inlineData),
141+
}
142+
}
143+
]
144+
},
145+
'finishReason': 'STOP',
146+
}
147+
],
148+
};
149+
final response =
150+
DeveloperSerialization().parseGenerateContentResponse(jsonResponse);
151+
final part = response.candidates.first.content.parts.first;
152+
expect(part, isA<InlineDataPart>());
153+
expect((part as InlineDataPart).mimeType, 'application/octet-stream');
154+
expect(part.bytes, inlineData);
155+
});
124156
});
125157
});
126158
}

0 commit comments

Comments
 (0)