Repository navigation
feat(ai): send image attachment labels to Gemini - #16919
andrewheard wants to merge 1 commit into
Conversation
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. |
1d01ed5 to
775993a
Compare
Send each image attachment's label to the model as a text part, such as `[barcode-image]`, right before the image. Previously, labels weren't sent, so the model couldn't refer to an image by its label. Tools that take an `ImageReference`, such as Vision's `BarcodeReaderTool` and `OCRTool`, resolve an image in the transcript by its label, and their descriptions say the label is "the identifier shown with the image". Without labels, Gemini guessed names such as `input_file_0.png` or `image_0`, and `respond` threw `LanguageModelSession.ToolCallError` whenever the prompt didn't mention the label. With labels sent, these tools resolve labeled images whether or not the prompt mentions the label, matching the on-device model. As with the on-device model, tools still can't refer to unlabeled images, since Foundation Models doesn't assign labels to attachments. Add unit tests for the label parts and an integration test that runs both tools on one and two labeled images whose labels the prompt doesn't mention.
775993a to
3a4a31f
Compare
GeminiLanguageModelnow sends each image attachment's label to the model as a text part, such as[barcode-image], right before the image. This makes tools that take anImageReference, such as Vision'sBarcodeReaderToolandOCRTool, work with labeled images, the same way they do with the on-device model.Motivation
Tools that take an
ImageReferencefind an image in the transcript by its attachment label. Their tool descriptions tell the model that the label is "the identifier shown with the image". #16895 didn't send labels, so Gemini couldn't see them. Unless the prompt text happened to mention the label, Gemini made one up (for example,input_file_0.png,image_0, orimg_0). The tool then threwinvalidImage("No image in transcript with label …"), andrespondfailed withLanguageModelSession.ToolCallError. That breaks Apple's ownBarcodeReaderToolexample, which labels the image but never mentions the label in the prompt.I compared both tools with one and two images across all three integration backends. On-device runs used
SystemLanguageModeldirectly, once per case:SystemLanguageModelAfter this change, Gemini behaves like the on-device model in every case. Unlabeled images fail on both, because Foundation Models doesn't assign labels to attachments. The
fmCLI only gets around this because it labels imagesimage_0,image_1, and so on itself.Changes
GeminiTranscriptTranslator: a labeled image in a prompt is preceded by a text part containing its label in square brackets. Unlabeled images, and images with an empty label, are sent exactly as before. The bracket format matches what the on-device model echoed back in one of the runs.GeminiLanguageModeldocs: thevisioncapability docs now describe how labels are sent, and that tools taking anImageReferencecan only refer to labeled attachments. The previous "labels aren't sent" limitation is removed.Testing
Details
GeminiTranscriptTranslatorTests): a label part right before its image; label parts placed between surrounding text and in order for multiple images; only labeled images get label parts; empty labels are skipped.ImageReferenceToolIntegrationTests):BarcodeReaderToolandOCRToolwith one and two labeled images, whose labels the prompt never mentions, on all three backends (12 cases). Each test checks the transcript, not just the final answer, since Gemini can read text and QR codes by itself:ToolCallError.swift-format lint --strictis clean.Usage examples
Read a barcode with
BarcodeReaderToolThis is Apple's documented example, unchanged. The prompt doesn't need to mention the label.
Gemini receives
[barcode-image]right before the image and calls the tool with{"image": {"attachmentLabel": "barcode-image"}}.Read text from several images with
OCRToolGive each image a distinct label. Gemini calls the tool once per image, using each image's own label.
Unlabeled images can't be used with these tools
Tools that take an
ImageReferencecan only find labeled images. This is true for the on-device model too. Without a label, the model makes one up, andrespondthrows:Label every image that a tool might need to refer to, for example with
Attachment(image).label("image-1").