[ContentUnderstanding] Add ContentRange samples for document, video, and audio.#45679
Open
changjian-wang wants to merge 2 commits intomainfrom
Open
[ContentUnderstanding] Add ContentRange samples for document, video, and audio.#45679changjian-wang wants to merge 2 commits intomainfrom
changjian-wang wants to merge 2 commits intomainfrom
Conversation
added 2 commits
March 12, 2026 18:36
- Implemented ContentRange functionality in sample scripts for analyzing binary documents and URLs. - Added examples for analyzing specific pages and combined page ranges in `sample_analyze_binary.py`. - Enhanced `sample_analyze_url.py` with ContentRange examples for documents, videos, and audio, including time-based ranges. - Created unit tests for ContentRange functionality, covering various scenarios and edge cases. - Updated existing tests to validate ContentRange behavior in document and media analysis.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a new ContentRange value type for the Azure AI Content Understanding SDK, enabling users to specify content ranges (document pages or audio/video time intervals) when analyzing content. The class provides factory methods for constructing ranges and is integrated into the begin_analyze_binary API.
Changes:
- Added
ContentRangeclass with factory methods (page,pages,pages_from,time_range,time_range_from,combine) and exported it in the models namespace. - Updated
begin_analyze_binary(sync and async) to acceptContentRangeobjects in addition to raw strings for thecontent_rangeparameter. - Added comprehensive sample code and tests demonstrating
ContentRangeusage for document, video, and audio analysis scenarios.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
models/_content_range.py |
New ContentRange class with factory methods, equality, hashing, and string conversion |
models/_patch.py |
Exports ContentRange in __all__ |
_patch.py |
Updates sync begin_analyze_binary to accept ContentRange and convert to string |
aio/_patch.py |
Updates async begin_analyze_binary to accept ContentRange and convert to string |
tests/test_content_range.py |
Unit tests for ContentRange construction, validation, equality, and integration with AnalysisInput |
tests/samples/test_sample_analyze_url.py |
Integration tests for ContentRange with document, video, and audio URL analysis |
tests/samples/test_sample_analyze_binary.py |
Integration tests for ContentRange with binary document analysis |
samples/sample_analyze_url.py |
Sync sample showing ContentRange usage for URL-based analysis |
samples/async_samples/sample_analyze_url_async.py |
Async sample showing ContentRange usage for URL-based analysis |
samples/sample_analyze_binary.py |
Sync sample showing ContentRange usage for binary analysis |
samples/async_samples/sample_analyze_binary_async.py |
Async sample showing ContentRange usage for async binary analysis |
assets.json |
Updated asset tag for new test recordings |
| tests_dir = os.path.dirname(os.path.dirname(__file__)) | ||
| file_path = os.path.join(tests_dir, "test_data", "mixed_financial_docs.pdf") | ||
| if not os.path.exists(file_path): | ||
| file_path = os.path.join(tests_dir, "test_data", "sample_invoice.pdf") |
Comment on lines
+23
to
+40
| range = ContentRange.page(5) # "5" | ||
| range = ContentRange.pages(1, 3) # "1-3" | ||
| range = ContentRange.pages_from(9) # "9-" | ||
|
|
||
| # Audio/video time ranges | ||
| range = ContentRange.time_range( | ||
| timedelta(0), timedelta(seconds=5)) # "0-5000" | ||
| range = ContentRange.time_range_from( | ||
| timedelta(seconds=5)) # "5000-" | ||
|
|
||
| # Combine multiple ranges | ||
| range = ContentRange.combine( | ||
| ContentRange.pages(1, 3), | ||
| ContentRange.page(5), | ||
| ContentRange.pages_from(9)) # "1-3,5,9-" | ||
|
|
||
| # Or construct from a raw string | ||
| range = ContentRange("1-3,5,9-") |
Comment on lines
+50
to
+51
| if value is None: | ||
| raise ValueError("value cannot be None.") |
| """ContentRange value type for specifying content ranges on AnalysisInput.""" | ||
|
|
||
| from datetime import timedelta | ||
| from typing import Optional |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This pull request introduces a new
ContentRangevalue type for specifying content ranges in analysis requests, and updates both the SDK and sample code to support and demonstrate its usage. The changes improve flexibility for users to restrict analysis to specific document pages or time ranges in audio/video content, and update documentation and sample code to showcase these features.ContentRange value type introduction and integration
ContentRangeclass inmodels/_content_range.py, providing methods to construct ranges for document pages and audio/video time intervals, combine multiple ranges, and convert to string. This enables precise specification of content segments to analyze.ContentRangeinto the SDK: updatedbegin_analyze_binaryand its async variant to accept either a string or aContentRangeobject for thecontent_rangeparameter, converting it to string as needed. Documentation for these methods was updated to reflect the new parameter type and usage. [1] [2] [3] [4] [5] [6]ContentRangeinmodels/_patch.pyfor public access. [1] [2]Sample code updates
sample_analyze_binary_async.pyto demonstrate analyzing specific pages and combined page ranges using the newContentRangeclass, including example output for these scenarios. [1] [2]sample_analyze_url_async.pyto show how to restrict analysis to a single page usingContentRange, and changed the document URL to a more complex sample. Also importedtimedeltafor potential time range usage. [1] [2] [3] [4]Asset metadata update
assets.jsonto reflect the new version, ensuring asset tracking aligns with these changes.