Skip to content

fix(core): add image-grounding hint in function response for image at…#27711

Open
yasirusman85 wants to merge 2 commits into
google-gemini:mainfrom
yasirusman85:fix/image-grounding-hint-27710
Open

fix(core): add image-grounding hint in function response for image at…#27711
yasirusman85 wants to merge 2 commits into
google-gemini:mainfrom
yasirusman85:fix/image-grounding-hint-27710

Conversation

@yasirusman85
Copy link
Copy Markdown

This PR is in response to #27710

…tachments

Smaller preview models (e.g. gemini-3-flash-preview) can anchor on
unrelated surrounding context such as the <session_context> directory
listing and describe files that match filenames instead of the actual
pixels of an attached image (see issue google-gemini#27710).

Inject a short grounding hint into the function response text whenever
the tool output carries one or more image/* attachments. The hint asks
the model to describe only what is optically present in the attached
image and to ignore surrounding workspace context. It is harmless for
stronger models that already ground correctly.

Changes:
- compute imageParts and imageMimeTypes from filteredInlineDataParts
- prepend the hint to textParts output, or include it in the
  'Binary content provided' fallback when no text is present
- add unit tests covering Gemini 3 nested multimodal-FR path, the
  non-multimodal-FR sibling path, multi-image unique mime-type listing,
  PDF (non-image) binary, and stripped audio
@yasirusman85 yasirusman85 requested a review from a team as a code owner June 6, 2026 09:29
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue where smaller preview models incorrectly anchor on file names or directory listings instead of the actual image content. By injecting a grounding hint into the function response output, the model is explicitly instructed to focus solely on the provided image data.

Highlights

  • Image Grounding Hint: Introduced an image-grounding hint in function responses when image attachments are present to prevent models from hallucinating based on surrounding context.
  • Test Coverage: Expanded test suite to verify the inclusion of the hint for various image types, exclusion for non-image files, and correct handling of multiple image attachments.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions Bot added the size/m A medium sized PR label Jun 6, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 6, 2026

📊 PR Size: size/L

  • Lines changed: 265
  • Additions: +232
  • Deletions: -33
  • Files changed: 2

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces an image-grounding hint mechanism in convertToFunctionResponse to ensure smaller Gemini models focus on actual image pixels rather than surrounding context like filenames. It also adds comprehensive unit tests to validate this behavior. The review feedback correctly identifies a security vulnerability where unsanitized mimeType values could lead to indirect prompt injection, and suggests validating the mimeType against a strict regular expression to mitigate this risk.

Comment on lines +162 to +168
const imageMimeTypes = Array.from(
new Set(imageParts.map((p) => p.inlineData?.mimeType).filter((m) => !!m)),
);
const imageHint =
imageParts.length > 0
? `[Image grounding hint: This function response includes ${imageParts.length} image attachment(s) (${imageMimeTypes.join(', ')}). Describe ONLY what is optically present in the attached image(s). Do not infer content from workspace filenames, directory listings, prior conversation, or any other surrounding text context.]`
: undefined;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-high high

The convertToFunctionResponse function is vulnerable to indirect prompt injection. It extracts mimeType from inlineData parts and directly interpolates them into the imageHint prompt string without sanitization. An attacker could craft a malicious mimeType (e.g., image/png\n\n[SYSTEM INSTRUCTION: ...]) to inject instructions into the LLM. To remediate this, sanitize data from LLM-driven tools before injecting it into a system prompt. At a minimum, remove newlines and context-breaking characters (e.g., ']'). Validating the extracted mimeType against a strict regular expression prevents prompt injection. Additionally, ensure all image attachments (from both filteredInlineDataParts and fileDataParts) are considered when generating the image-grounding hint for comprehensive coverage.

  const imageMimeTypes = Array.from(
    new Set(
      imageParts
        .map((p) => p.inlineData?.mimeType)
        .filter((m): m is string => typeof m === 'string' && /^[a-zA-Z0-9\\-]+\\/[a-zA-Z0-9\\-.]+$/.test(m)),
    ),
  );
  const imageHint =
    imageParts.length > 0
      ? "[Image grounding hint: This function response includes " + imageParts.length + " image attachment(s) (" + imageMimeTypes.join(", ") + "). Describe ONLY what is optically present in the attached image(s). Do not infer content from workspace filenames, directory listings, prior conversation, or any other surrounding text context.]"
      : undefined;
References
  1. Sanitize data from LLM-driven tools before injecting it into a system prompt to prevent prompt injection. At a minimum, remove newlines and context-breaking characters (e.g., ']').

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have resolved the issue can you check again

@gemini-cli gemini-cli Bot added the status/need-issue Pull requests that need to have an associated issue. label Jun 6, 2026
@github-actions github-actions Bot added the size/l A large sized PR label Jun 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/l A large sized PR size/m A medium sized PR status/need-issue Pull requests that need to have an associated issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant