Skip to content

feat(gemini): Add support for RAG documents in dynamic context#1205

Merged
joshua-mo-143 merged 8 commits into0xPlaygrounds:mainfrom
snaumov:feature/gemini-rag-documents-support
Mar 3, 2026
Merged

feat(gemini): Add support for RAG documents in dynamic context#1205
joshua-mo-143 merged 8 commits into0xPlaygrounds:mainfrom
snaumov:feature/gemini-rag-documents-support

Conversation

@snaumov
Copy link
Copy Markdown
Contributor

@snaumov snaumov commented Jan 4, 2026

Add RAG Documents Support to Gemini Provider

Summary

This PR adds support for RAG (Retrieval-Augmented Generation) documents in the Gemini provider's dynamic_context() functionality. Previously, documents retrieved from vector stores were silently ignored, causing RAG agents to fail.

Problem

When using .dynamic_context() with Gemini agents, the retrieved documents were never passed to the Gemini API. The create_request_body() function in the Gemini provider ignored the documents field from CompletionRequest, making it impossible to use RAG with Gemini.

Solution

Changes Made

  1. Document Injection: Modified create_request_body() to check for documents and inject them as a user message at the beginning of chat history using the existing normalized_documents() helper method.

  2. Text Document Handling: Added special handling for DocumentMediaType::TXT and other text-based documents to convert them to plain text parts (PartKind::Text) instead of trying to send them as base64-encoded inline data.

  3. Backward Compatibility: Preserved existing behavior for other document types (images, PDFs, etc.) that use InlineData or FileData.

Code Changes

File: rig/rig-core/src/providers/gemini/completion.rs

  • Lines ~189-195: Added document injection logic
  • Lines ~773-838: Added TXT and other text-based document to text conversion

Testing

Tested with a real-world RAG application:

  • Application: Obsidian vault note organizer with 175+ markdown notes
  • Vector Store: In-memory vector store with Ollama embeddings (nomic-embed-text)
  • Query: "<some_query>"
  • Result: Successfully retrieved 10 relevant notes and Gemini correctly received and processed them

Before Fix

=== NOTES IN CONTEXT ===
NO NOTES IN CONTEXT
=== END OF NOTES ===

After Fix

=== NOTES IN CONTEXT ===
<list_of_notes>
=== END OF NOTES ===

Impact

  • ✅ Enables RAG functionality for Gemini provider
  • ✅ No breaking changes
  • ✅ Maintains backward compatibility
  • ✅ Follows existing patterns (uses normalized_documents())

Additional Notes

The fix aligns Gemini's behavior with other providers like Cohere and Anthropic that already support documents in RAG scenarios.

Copy link
Copy Markdown
Contributor

@joshua-mo-143 joshua-mo-143 left a comment

Choose a reason for hiding this comment

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

Hey, apologies for the late review - I meant to get around to this but I wasn't able to do so.

Needs cargo fmt then lgtm

This commit adds support for passing RAG documents to the Gemini API when
using dynamic_context() with agents. Previously, documents retrieved from
vector stores were ignored by the Gemini provider, causing RAG systems to
fail silently.

Changes:
- Modified create_request_body() to inject documents as user messages
  at the beginning of chat history
- Added special handling for DocumentMediaType::TXT to convert text documents
  to plain text parts instead of base64-encoded inline data
- Preserved existing behavior for other document types (images, PDFs, etc.)

Fixes:
- Documents from dynamic_context() now properly reach the Gemini model
- RAG-based agents can now successfully retrieve and use context from
  vector stores

Testing:
- Tested with a note-taking RAG system using Obsidian vault notes
- Verified that documents are correctly passed and interpreted by Gemini
- Confirmed backward compatibility with existing document handling
Added comprehensive tests for the new document handling functionality:

1. test_txt_document_conversion_to_text_part: Verifies that TXT documents
   are converted to PartKind::Text instead of InlineData

2. test_create_request_body_with_documents: Tests that documents are
   correctly injected into chat history at the beginning

3. test_create_request_body_without_documents: Ensures backward
   compatibility when no documents are present

All tests pass successfully.
@snaumov snaumov force-pushed the feature/gemini-rag-documents-support branch from f960195 to c0c6b31 Compare February 1, 2026 02:46
@snaumov
Copy link
Copy Markdown
Contributor Author

snaumov commented Feb 1, 2026

Hey, apologies for the late review - I meant to get around to this but I wasn't able to do so.

Needs cargo fmt then lgtm

updated, thank you!

@snaumov snaumov requested a review from joshua-mo-143 February 1, 2026 02:47
@atellou
Copy link
Copy Markdown
Contributor

atellou commented Feb 4, 2026

@snaumov I am working on a feature, and this solved it for me. I was getting random result till I used your branch. Thank you. Looking forward to seeing this merged so my own PR actually works

@snaumov
Copy link
Copy Markdown
Contributor Author

snaumov commented Feb 4, 2026

@atellou glad to hear it helped. @joshua-mo-143 let me know please if there is anything else to be done before we can merge

1 similar comment
@snaumov
Copy link
Copy Markdown
Contributor Author

snaumov commented Feb 4, 2026

@atellou glad to hear it helped. @joshua-mo-143 let me know please if there is anything else to be done before we can merge

@joshua-mo-143
Copy link
Copy Markdown
Contributor

Hey - sorry been meaning to get around to this but haven't kept my eye on it. I'm currently travelling but will have a look when I'm available - doesn't look like a lot needs to be done if anything iirc

1 similar comment
@joshua-mo-143
Copy link
Copy Markdown
Contributor

Hey - sorry been meaning to get around to this but haven't kept my eye on it. I'm currently travelling but will have a look when I'm available - doesn't look like a lot needs to be done if anything iirc

atellou pushed a commit to atellou/rig that referenced this pull request Mar 3, 2026
…xPlaygrounds#1205

- Convert text-based documents (TXT, Markdown, HTML, etc.) to plain text parts instead of inline data
- Inject documents as user message at beginning of chat history in create_request_body
- Add base64 decoding for text documents when needed
- Fix TypedPromptResponse to use `usage` field instead of `total_usage`

Includes comprehensive tests for:
- TXT and Markdown document conversion to text parts
- Document injection into request
@atellou
Copy link
Copy Markdown
Contributor

atellou commented Mar 3, 2026

I created a version of this PR up to date with the main branch and included my change: #1456

@joshua-mo-143
Copy link
Copy Markdown
Contributor

I created a version of this PR up to date with the main branch and included my change: #1456

I'll be merging this PR first. Trying to solve multiple issues with a single PR is generally not a great idea.

@atellou
Copy link
Copy Markdown
Contributor

atellou commented Mar 3, 2026

Works for me

@joshua-mo-143 joshua-mo-143 added this pull request to the merge queue Mar 3, 2026
Merged via the queue into 0xPlaygrounds:main with commit 99df454 Mar 3, 2026
6 checks passed
@joshua-mo-143
Copy link
Copy Markdown
Contributor

joshua-mo-143 commented Mar 3, 2026

Finally merged!

@snaumov Thanks for your hard work on the initial implementation of this, I know it's taken a while to get this merged. I have made necessary changes and merged this PR as this was supposed to be merged at an earlier point but I got distracted and kept forgetting.

@github-actions github-actions Bot mentioned this pull request Mar 3, 2026
@snaumov
Copy link
Copy Markdown
Contributor Author

snaumov commented Mar 7, 2026

@joshua-mo-143 thank you for pushing it across finish line! Sorry got distracted myself and didn't rebase on time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants