Added support for video via Gemini and LiteLM #9117
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.
Goal
Approach
dspy.Videocustom type that follows DSPy’s existing media-type pattern (Image/Audio), so signatures and adapters can serialize video content into OpenAI-style blocks that LiteLLM maps to Gemini’s Files API.file_ids.litellm.create_filewithcustom_llm_provider="gemini"andGEMINI_API_KEY, handling MIME detection and a 20MB inline guard.Files Touched (why)
dspy/adapters/types/video.py: NewVideotype, MIME helpers, inline size guard, upload methods (path and in-memory), formatting to{"type": "file", "file": ...}blocks, YouTube handling, repr/str.dspy/__init__.py,dspy/adapters/__init__.py,dspy/adapters/types/__init__.py: ExportVideopublicly.docs/docs/api/primitives/Video.md: API reference fordspy.Video.docs/docs/tutorials/video/index.md: Gemini-focused tutorial and recipes.docs/docs/learn/programming/signatures.md: Example showingdspy.Video; update special-type list.docs/mkdocs.yml: Add tutorial and API nav entries.docs/scripts/generate_api_docs.py: Includedspy.Videoin generated API.tests/adapters/test_video.py: Unit tests for helpers, constructors, formatting, immutability, encode helper.tests/signatures/test_adapter_video.py: Signature integration (single/list/optional), mixed media with Image, save/load, ChainOfThought.Test Runs
pytest tests/adapters/test_video.py tests/signatures/test_adapter_video.pyReviewer Validation Guide
pytest tests/adapters/test_video.py tests/signatures/test_adapter_video.py.GEMINI_API_KEYand LiteLLM with Files API):filecontent block in the request and a text summary back.dspy.Video.upload_from_path("<path>", mime_type="video/mp4")should return aVideowithfile_id; run a simple predict using that instance.mkdocs serveand confirm Video tutorial and API pages appear in nav.Rationale for the
dspy.VideoApproachPerspective and Potential Follow-ups
format()uses{"type": "file", "file": {"file_data": <url|data>, ...}}and setsformat: "video/mp4"for YouTube. Confirm against live Gemini whetherfile_urivsfile_datais preferred and whether to omitformatfor YouTube to avoid implying mp4.create_file; see their docs: https://docs.litellm.ai/docs/providers/google_ai_studio/files. Cross-check with Google’s Gemini video understanding guidance: https://ai.google.dev/gemini-api/docs/video-understanding#upload-video.upload()will download arbitrary URLs withrequests.get; consider requiring local path/data URI for upload or making remote fetch opt-in with a size check.adapt_to_native_lm_featureto flag Gemini video capability if we want more automatic behavior.upload_from_pathreads whole files; streaming/chunking would be safer for very large videos.requestsis absent, remote-upload path will fail; document or guard accordingly.