-
Notifications
You must be signed in to change notification settings - Fork 2.2k
docs: add file upload handling pattern guide #3193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: add file upload handling pattern guide #3193
Conversation
Add common_patterns.md with best practices for handling both uploaded files and file path strings in agent tools. This pattern helps agents gracefully process files regardless of upload method. Also update contributing/README.md to reference the new common patterns guide. #non-breaking
Summary of ChangesHello @jpantsjoha, 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 introduces a new documentation guide aimed at standardizing and improving how ADK agents handle file uploads. The guide provides best practices for tools to accept both directly uploaded files and file paths, along with explicit agent instructions to prevent common pitfalls such as redundant path requests or failure to recognize attachments. This enhancement ensures agents can gracefully process files regardless of the upload method, contributing to more robust multi-agent solution architectures. Highlights
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 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 counter productive. 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. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this 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 adds a valuable new guide for common development patterns, starting with file upload handling. The documentation is well-structured and the addition to the main contributing README improves discoverability. I've identified an issue in the Python code example within common_patterns.md where the logic for generating a run_id from a file path is incorrect and could lead to unexpected behavior. I've provided a suggestion to make it more robust and intuitive. Overall, this is a great addition to the project's documentation.
contributing/common_patterns.md
Outdated
| if '/' in doc_path: | ||
| # Full path provided | ||
| run_id = os.path.basename(os.path.dirname(doc_path)) | ||
| else: | ||
| # Uploaded file display name or simple identifier | ||
| run_id = doc_path.replace('.', '_') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for generating run_id seems incorrect and could lead to unexpected behavior for a few reasons:
- The check
if '/' in doc_path:is not portable and will not work correctly on Windows systems, which use\as a path separator. - For a full file path,
os.path.basename(os.path.dirname(doc_path))extracts the parent directory's name instead of the file's name. For example, with/path/to/reports/quarterly_report.pdf,run_idwould becomereports, which is likely not the intended behavior.
A more robust and intuitive approach is to extract the file's base name and then remove the extension. This works consistently for both full paths and simple display names, across different operating systems.
| if '/' in doc_path: | |
| # Full path provided | |
| run_id = os.path.basename(os.path.dirname(doc_path)) | |
| else: | |
| # Uploaded file display name or simple identifier | |
| run_id = doc_path.replace('.', '_') | |
| # Get the base name, which is the final component of the path (e.g., "file.pdf"). | |
| base_name = os.path.basename(doc_path) | |
| # Use the name without extension as the run_id (e.g., "file"). | |
| run_id, _ = os.path.splitext(base_name) |
Address Gemini Code Assist feedback by using os.path.basename() and os.path.splitext() for robust file name extraction. This fixes: - Platform incompatibility (Windows backslash separators) - Incorrect extraction (parent directory instead of filename) - Cleaner run_id generation across all platforms #non-breaking
|
Thank you @gemini-code-assist for the thorough review! I've applied the suggested fix using
The pattern now works consistently across all operating systems and for both full paths and display names. |
|
You're very welcome, @jpantsjoha! I'm glad to hear that the suggestions were helpful and that the updated approach using |
|
Hi @jpantsjoha , could you move these content to adk-docs repo? That's for public documentation. |
Add comprehensive best practices guide covering: - File upload handling patterns (both uploaded files and file paths) - Type annotation requirements - Tool naming conventions - Docstring standards - Error handling patterns - Structured return values Addresses common pitfalls and provides examples for building robust function tools in ADK. Relocates content from google/adk-python#3193 as requested by @boyangsvl.
|
Closing this PR as requested by @boyangsvl. The documentation content has been relocated to the correct repository. New PR: google/adk-docs#785 Thank you for the guidance on proper repository structure! |
Add comprehensive best practices guide covering: - File upload handling patterns (both uploaded files and file paths) - Type annotation requirements - Tool naming conventions - Docstring standards - Error handling patterns - Structured return values Addresses common pitfalls and provides examples for building robust function tools in ADK. Relocates content from google/adk-python#3193 as requested by @boyangsvl.
Summary
Adds a new
contributing/common_patterns.mdguide with best practices for handling file uploads in agent tools.Motivation
While building multi-agent solution architectures, discovered that handling both uploaded files and file path strings is a common pattern that isn't documented. This pattern helps agents gracefully process files regardless of upload method.
Without explicit instructions, agents may:
Changes
contributing/common_patterns.mdwith file upload patterncontributing/README.mdto reference common patterns guideCode Example
The pattern demonstrates flexible file path handling:
And corresponding agent instructions:
Testing Plan
Questions for Reviewers
adk-docsrepo instead for better end-user visibility?Type
Related
This pattern was discovered while building enterprise multi-agent solution architectures using A2A protocol and has proven valuable for file processing workflows.
Tags: #non-breaking