feat: add batch operation validator to prevent incompatible mixing #2683
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds BatchOperationAnalyzer utility class to detect and prevent incompatible operation mixing in BatchHttpRequest, specifically addressing Issue #2655.
Problem Statement
When using BatchHttpRequest with Google Drive API, mixing certain operations causes a 400 error with message "This API does not support batching". Specifically, combining files.modifyLabels with operations like files.update, files.copy, or files.create in the same batch fails, even though each operation works individually.
Related Issue: #2655 (open for 54 days)
Solution
This PR introduces batch_utils.py with BatchOperationAnalyzer class that proactively analyzes batch operations before execution, detects known incompatible operation pairs, provides detailed warnings with actionable suggestions, and includes a suggest_batch_split() method that automatically splits incompatible batches into safe sub-batches.
Usage Example
Before (causes 400 error):
After (validates before execution):
Testing
Comprehensive test suite included with 16 unit tests covering all functionality, real-world scenario tests including exact Issue #2655 reproduction, edge cases, and production workflow validation. All tests pass: 16 passed in 0.08s
Production Validation
This utility has been battle-tested in PupiBot (AI agent for Google Workspace):
Design Decisions
Separate utility vs modifying BatchHttpRequest because: non-breaking (existing code continues working unchanged), opt-in (developers choose when to use validation), extensible (easy to add new incompatibilities), and lightweight (zero dependencies beyond standard library).
This is a server-side limitation in Google Drive API, not a client library bug. The utility provides a client-side workaround until Google documents/fixes the restriction.
Backward Compatibility
100% backward compatible - no changes to existing classes, no breaking changes, pure addition of new utility module.
Documentation
Comprehensive docstrings (Google style), usage examples in docstrings, real-world scenario documentation, and type hints for IDE support.
Attribution
This solution was developed while building PupiBot, an AI-powered automation agent for Google Workspace. The complete AI orchestration layer remains proprietary, but this specific utility is contributed to benefit the community.
Author: Claudio Gallardo
License: Apache 2.0 (same as google-api-python-client)
Checklist
Note to Maintainers: Happy to adjust file location or make any requested changes. Available for questions!