DSPy-style framework for optimizing text-to-video generation via VBench metric feedback
Project description
ViDSPy ๐ฌ
DSPy-style framework for optimizing text-to-video prompts via VBench metric feedback.
ViDSPy brings the power of DSPy's declarative programming paradigm to text-to-video generation. Optimize your prompts for any text-to-video API (Runway, Pika, Replicate, etc.) using VBench quality metrics and VLM-based feedback.
Note: ViDSPy optimizes how you prompt video generation models. It does not generate videos itself - you bring your own text-to-video API.
๐ฏ Key Features
- DSPy-style Optimization: Tune instructions (prompt templates) and demonstrations (few-shot examples) using canonical DSPy optimizers
- VBench Integration: Full support for all 10 CORE_METRICS from VBench evaluation
- Multiple Optimizers: BootstrapFewShot, LabeledFewShot, MIPROv2, COPRO, and GEPA
- Flexible VLM Backends: OpenRouter (cloud) and HuggingFace (local) support
- Composite Metrics: Weighted combination of video quality (60%) and text-video alignment (40%)
๐ฆ Installation
pip install vidspy
For full functionality with VBench evaluation:
pip install vidspy[vbench]
For development:
pip install vidspy[all]
โ๏ธ Configuration
ViDSPy can be configured in two ways:
Option 1: Pass Arguments Directly in Code
from vidspy import ViDSPy
vidspy = ViDSPy(
vlm_backend="openrouter",
vlm_model="google/gemini-2.5-flash",
api_key="your-api-key",
device="auto"
)
Option 2: Use Configuration File
Create a vidspy_config.yaml file from the template:
cp vidspy_config.yaml.example vidspy_config.yaml
Edit the configuration file:
# vidspy_config.yaml
vlm:
backend: openrouter
model: google/gemini-2.5-flash
optimizer:
lm: openai/gpt-4o-mini # LLM for generating instruction variations
optimization:
default_optimizer: mipro_v2
max_bootstrapped_demos: 4
metrics:
quality_weight: 0.6
alignment_weight: 0.4
cache:
dir: ~/.cache/vidspy
hardware:
device: auto
dtype: float16
Then use ViDSPy without any arguments - it automatically loads the config:
from vidspy import ViDSPy, VideoChainOfThought, Example
# ViDSPy automatically finds and loads vidspy_config.yaml
vidspy = ViDSPy()
# All settings from the config file are now applied!
trainset = [Example(prompt="a cat jumping", video_path="cat.mp4")]
optimized = vidspy.optimize(VideoChainOfThought("prompt -> video"), trainset)
Config file search order:
ViDSPy automatically searches for vidspy_config.yaml in:
- Current working directory:
./vidspy_config.yaml - User config directory:
~/.vidspy/config.yaml - User home directory:
~/vidspy_config.yaml
Custom config path:
You can also specify a custom config file location:
vidspy = ViDSPy(config_path="/path/to/custom_config.yaml")
Important:
- Arguments passed directly to
ViDSPy()always override config file values - API keys should be in environment variables or
.envfile (not in the config file):
# .env
OPENROUTER_API_KEY=your-api-key-here
OPENROUTER_OPTIMIZER_API_KEY=your-api-key-here # For optimizer LLM
๐ค Optimizer LLM Configuration
ViDSPy uses two different LLMs:
- VLM (Vision Language Model): Analyzes videos and enhances prompts during inference
- Optimizer LLM: Used by MIPROv2, COPRO, and GEPA optimizers to generate instruction variations during optimization
Note: BootstrapFewShot and LabeledFewShot optimizers do NOT require an optimizer LLM - they work without this configuration.
Configure the Optimizer LLM:
from vidspy import ViDSPy
vidspy = ViDSPy(
vlm_backend="openrouter",
vlm_model="google/gemini-2.5-flash", # For video analysis
optimizer_lm="openai/gpt-4o-mini", # For optimization
optimizer_api_key="your-openrouter-api-key" # Or set OPENROUTER_OPTIMIZER_API_KEY
)
Or in vidspy_config.yaml:
vlm:
backend: openrouter
model: google/gemini-2.5-flash
optimizer:
lm: openai/gpt-4o-mini # Any OpenRouter model
Choosing an Optimizer LLM:
- For most users:
openai/gpt-4o-mini(fast and cost-effective) - For better quality:
openai/gpt-4ooranthropic/claude-3-5-sonnet - Any model from OpenRouter is supported
Note: If optimizer_api_key is not specified, it will use the VLM API key as fallback.
๐ฅ Connecting Video Generation Models
Important: ViDSPy is a prompt optimization framework that sits on top of existing text-to-video models. It does NOT generate videos itself. Instead, it optimizes how you prompt external video generation services.
How ViDSPy Works
User Prompt โ ViDSPy (optimize prompt) โ Text-to-Video API โ Generated Video
โ
VBench + VLM (evaluate quality)
โ
Learn better prompting strategies
Setting Up Your Video Generator
You need to provide a video_generator function that connects to your preferred text-to-video service:
Example 1: Runway Gen-3
from vidspy import VideoChainOfThought
def runway_generator(prompt: str, **kwargs) -> str:
"""Generate video using Runway Gen-3 API."""
import requests
response = requests.post(
"https://api.runwayml.com/v1/generate",
headers={"Authorization": f"Bearer {RUNWAY_API_KEY}"},
json={
"prompt": prompt,
"model": "gen3",
"duration": kwargs.get("duration", 5)
}
)
video_url = response.json()["output"]["url"]
# Download and save video locally
video_path = f"outputs/{response.json()['id']}.mp4"
# ... download logic ...
return video_path
# Create module with your generator
module = VideoChainOfThought(
"prompt -> video",
video_generator=runway_generator
)
Example 2: Replicate (Stable Video Diffusion, CogVideo, etc.)
import replicate
def replicate_generator(prompt: str, **kwargs) -> str:
"""Generate video using Replicate API."""
output = replicate.run(
"stability-ai/stable-video-diffusion",
input={"prompt": prompt}
)
# Save video from output URL
video_path = f"outputs/{uuid.uuid4()}.mp4"
# ... download logic ...
return video_path
module = VideoChainOfThought(
"prompt -> video",
video_generator=replicate_generator
)
Example 3: Pika Labs
from pika import PikaClient
def pika_generator(prompt: str, **kwargs) -> str:
"""Generate video using Pika Labs API."""
client = PikaClient(api_key=PIKA_API_KEY)
video = client.generate_video(
prompt=prompt,
aspect_ratio="16:9",
duration=3
)
return video.download_path
module = VideoChainOfThought(
"prompt -> video",
video_generator=pika_generator
)
Supported Text-to-Video Services
ViDSPy works with any text-to-video API. Popular options include:
| Service | API Available | Notes |
|---|---|---|
| Runway Gen-3 | โ Yes | High quality, good motion |
| Pika Labs | โ Yes | Creative effects, good for social media |
| Stability AI Video | โ Yes | Open weights available |
| Replicate | โ Yes | Multiple models (CogVideo, SVD, etc.) |
| LumaAI Dream Machine | โ Yes | Cinematic quality |
| HaiperAI | โ Yes | Fast generation |
| Morph Studio | โ Yes | Style control |
Custom Video Generator Template
def my_video_generator(prompt: str, **kwargs) -> str:
"""
Your custom video generation function.
Args:
prompt: Enhanced prompt from ViDSPy
**kwargs: Additional parameters (duration, aspect_ratio, etc.)
Returns:
Local path to the generated video file
"""
# 1. Call your text-to-video API
# 2. Download the generated video
# 3. Save it locally
# 4. Return the file path
video_path = "path/to/generated/video.mp4"
return video_path
๐ Quick Start
from vidspy import ViDSPy, VideoChainOfThought, Example
# Step 1: Define your video generator (connect to your text-to-video API)
def my_video_generator(prompt: str, **kwargs) -> str:
"""Your text-to-video API integration."""
# Example: Runway, Pika, Replicate, etc.
import my_video_api
video = my_video_api.generate(prompt=prompt)
return video.save_path()
# Step 2: Initialize ViDSPy with OpenRouter VLM backend
# Note: Optimizer LLM defaults to "openai/gpt-4o-mini" via OpenRouter
# You can customize: ViDSPy(vlm_backend="openrouter", optimizer_lm="openai/gpt-4o")
vidspy = ViDSPy(vlm_backend="openrouter")
# Step 3: Create training examples (use videos you've already generated)
trainset = [
Example(prompt="a cat jumping over a fence", video_path="cat_jump.mp4"),
Example(prompt="a dog running in a park", video_path="dog_run.mp4"),
Example(prompt="a bird flying through clouds", video_path="bird_fly.mp4"),
]
# Step 4: Create module with your video generator
module = VideoChainOfThought(
"prompt -> video",
video_generator=my_video_generator # Connect your generator!
)
# Step 5: Optimize prompting strategy
optimized = vidspy.optimize(
module,
trainset,
optimizer="mipro_v2" # Multi-stage instruction + demo optimization
)
# Step 6: Generate videos with optimized prompts
result = optimized("a dolphin swimming in the ocean")
print(f"Generated video: {result.video_path}")
print(f"Optimized prompt used: {result.enhanced_prompt}")
๐ VBench Metrics
ViDSPy uses VBench's 10 CORE_METRICS split into two categories:
Video Quality Metrics (60% weight, video-only)
| Metric | Description |
|---|---|
subject_consistency |
Temporal stability of subjects |
motion_smoothness |
Natural motion quality |
temporal_flickering |
Absence of temporal jitter |
human_anatomy |
Correct hands/faces/torso rendering |
aesthetic_quality |
Artistic/visual beauty |
imaging_quality |
Technical clarity and sharpness |
Text-Video Alignment Metrics (40% weight, prompt-conditioned)
| Metric | Description |
|---|---|
object_class |
Prompt objects appear correctly |
human_action |
Prompt actions performed correctly |
spatial_relationship |
Correct spatial layout |
overall_consistency |
Holistic text-video alignment |
Using Metrics
from vidspy.metrics import composite_reward, quality_score, alignment_score
# Default composite metric (60% quality + 40% alignment)
score = composite_reward(example, prediction)
# Quality-only score
q_score = quality_score(example, prediction)
# Alignment-only score
a_score = alignment_score(example, prediction)
# Custom metric configuration
from vidspy.metrics import VBenchMetric
custom_metric = VBenchMetric(
quality_weight=0.5,
alignment_weight=0.5,
quality_metrics=["motion_smoothness", "aesthetic_quality"],
alignment_metrics=["object_class", "overall_consistency"]
)
๐ง Optimizers
ViDSPy provides 5 DSPy-compatible optimizers:
| Optimizer | Description | Key Parameters |
|---|---|---|
VidBootstrapFewShot |
Auto-generate/select few-shots | max_bootstrapped_demos=4 |
VidLabeledFewShot |
Static few-shot assignment | k=3 |
VidMIPROv2 |
Multi-stage instruction + demo optimization | num_candidates=10, auto="light" |
VidCOPRO |
Cooperative multi-LM instruction optimization | breadth=5, depth=3 |
VidGEPA |
Generate + Evaluate + Propose + Accept | auto="light" |
Example: Using Different Optimizers
# Bootstrap few-shot
optimized = vidspy.optimize(
module, trainset,
optimizer="bootstrap",
max_bootstrapped_demos=4
)
# MIPROv2 with more candidates
optimized = vidspy.optimize(
module, trainset,
optimizer="mipro_v2",
num_candidates=15,
auto="medium"
)
# COPRO with custom search
optimized = vidspy.optimize(
module, trainset,
optimizer="copro",
breadth=10,
depth=5
)
๐ค VLM Providers
Vision Language Models (VLMs) in ViDSPy are used for:
- ๐ Prompt enhancement - Improving user prompts before generation
- ๐ Video analysis - Understanding generated video content
- ๐ฏ Quality assessment - Analyzing text-video alignment
- ๐ง Chain-of-thought reasoning - Planning video generation strategies
Note: VLMs do NOT generate videos. They help optimize the prompts you send to your text-to-video API.
OpenRouter (Default)
Cloud-based multimodal VLMs via unified API:
vidspy = ViDSPy(
vlm_backend="openrouter",
vlm_model="google/gemini-2.5-flash",
api_key="your-api-key" # Or set OPENROUTER_API_KEY env var
)
Example models:
google/gemini-2.5-flashgoogle/gemini-1.5-proanthropic/claude-opus-4.5openai/gpt-4o
HuggingFace (Local)
Local video VLMs for offline usage:
vidspy = ViDSPy(
vlm_backend="huggingface",
vlm_model="llava-hf/llava-v1.6-mistral-7b-hf",
device="cuda"
)
๐ Video Modules
ViDSPy provides several module types for different prompting strategies.
Module Signatures
When creating video modules, you'll typically use simple signature strings like "prompt -> video" or "prompt -> video_path". The library internally handles all the complex reasoning steps (scene analysis, motion planning, etc.) based on the module type you choose. You don't need to worry about the internal signature detailsโjust use these standard formats and ViDSPy takes care of the rest.
from vidspy import VideoPredict, VideoChainOfThought, VideoReAct, VideoEnsemble
# Define your video generator once
def my_video_gen(prompt, **kwargs):
# Your text-to-video API call
return video_path
# Simple prediction with prompt enhancement
# Just use "prompt -> video_path" - ViDSPy handles the internal complexity
predictor = VideoPredict(
"prompt -> video_path",
video_generator=my_video_gen
)
# Chain-of-thought reasoning (analyzes scene, motion, style before generating)
# The simple "prompt -> video" signature is all you need
cot = VideoChainOfThought(
"prompt -> video",
video_generator=my_video_gen
)
# ReAct-style iterative refinement (generates, evaluates, refines)
# Same simple signature - internal reasoning is handled automatically
react = VideoReAct(
"prompt -> video",
video_generator=my_video_gen,
max_iterations=3
)
# Ensemble multiple approaches (tries different strategies, picks best)
ensemble = VideoEnsemble([
VideoPredict(video_generator=my_video_gen),
VideoChainOfThought(video_generator=my_video_gen),
], selection_metric=composite_reward)
๐ ๏ธ Setup VBench Models
# Via CLI
vidspy setup
# Via Python
from vidspy import setup_vbench_models
setup_vbench_models() # Downloads to ~/.cache/vbench
๐ Full Example
import os
import replicate
from vidspy import (
ViDSPy,
VideoChainOfThought,
Example,
composite_reward,
)
# Set API key
os.environ["OPENROUTER_API_KEY"] = "your-api-key"
# Define video generator (using Replicate as example)
def replicate_video_generator(prompt: str, **kwargs) -> str:
"""Generate video using Replicate API."""
output = replicate.run(
"stability-ai/stable-video-diffusion",
input={"prompt": prompt}
)
# Download and save video
video_path = f"outputs/{hash(prompt)}.mp4"
# ... download logic ...
return video_path
# Initialize ViDSPy
vidspy = ViDSPy(vlm_backend="openrouter")
# Prepare training data (videos you've already generated)
trainset = [
Example(
prompt="a person walking through a forest",
video_path="data/walk_forest.mp4"
),
Example(
prompt="a car driving on a highway",
video_path="data/car_highway.mp4"
),
# ... more examples
]
# Split for validation
valset = trainset[-2:]
trainset = trainset[:-2]
# Create module with your video generator
module = VideoChainOfThought(
"prompt -> video",
video_generator=replicate_video_generator
)
# Optimize prompting strategy
optimized = vidspy.optimize(
module,
trainset,
valset=valset,
metric=composite_reward,
optimizer="mipro_v2",
num_candidates=10,
)
# Evaluate on test set
testset = [Example(prompt="a boat on a lake", video_path="data/boat.mp4")]
results = vidspy.evaluate(optimized, testset)
print(f"Mean Score: {results['mean_score']:.4f}")
print(f"Quality: {results['details'][0].get('quality_score', 'N/A')}")
print(f"Alignment: {results['details'][0].get('alignment_score', 'N/A')}")
# Generate new videos with optimized prompts
result = optimized("a butterfly landing on a flower")
print(f"Generated: {result.video_path}")
print(f"Enhanced prompt: {result.enhanced_prompt}")
๐ CLI Reference
# Show help
vidspy --help
# Setup VBench models
vidspy setup
vidspy setup --cache-dir /path/to/cache
# Check dependencies
vidspy setup --check-only
# Optimize a module
vidspy optimize trainset.json --optimizer mipro_v2 --output optimized_model
# Evaluate a module
vidspy evaluate testset.json --module optimized_model --output results.json
# Show information
vidspy info
๐๏ธ Project Structure
vidspy/
โโโ pyproject.toml # Package configuration
โโโ README.md # This file
โโโ .env.example # Environment variables template
โโโ vidspy_config.yaml.example # Configuration file template
โโโ vidspy/
โ โโโ __init__.py # Main exports
โ โโโ core.py # ViDSPy main class, Example
โ โโโ signatures.py # VideoSignature, etc.
โ โโโ modules.py # VideoPredict, VideoChainOfThought
โ โโโ optimizers.py # VidBootstrapFewShot, VidMIPROv2, etc.
โ โโโ metrics.py # VBench wrappers, composite_reward
โ โโโ providers.py # OpenRouterVLM, HuggingFaceVLM
โ โโโ setup.py # Setup utilities
โ โโโ cli.py # Command-line interface
โโโ examples/
โ โโโ basic_usage.py
โโโ tests/
โโโ test_basic.py
๐ฌ Target Thresholds
For production-quality videos, aim for:
- Human Anatomy: โฅ 0.85
- Text-Video Alignment: โฅ 0.80
๐ References
- DSPy - Declarative Self-improving Python
- VBench - Video generation benchmark
- OpenRouter - Unified AI API
๐ License
MIT License - see LICENSE for details.
๐ค Contributing
Contributions welcome! Please read our contributing guide first.
โญ Star History
If you find ViDSPy useful, please consider giving it a star!
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file vidspy-0.1.2.tar.gz.
File metadata
- Download URL: vidspy-0.1.2.tar.gz
- Upload date:
- Size: 47.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd6d956b30b0cfe26603b07fe2911dc569466a9ec44cb58b57d57c7260d9d26f
|
|
| MD5 |
72b76546aaf540a7940ab938d1b12f34
|
|
| BLAKE2b-256 |
9f4ca294c306dc5d47b151eb3e4da9262ceb6872a1f11dc7133c78ad4780a570
|
Provenance
The following attestation bundles were made for vidspy-0.1.2.tar.gz:
Publisher:
publish.yml on leockl/vidspy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
vidspy-0.1.2.tar.gz -
Subject digest:
cd6d956b30b0cfe26603b07fe2911dc569466a9ec44cb58b57d57c7260d9d26f - Sigstore transparency entry: 854918707
- Sigstore integration time:
-
Permalink:
leockl/vidspy@97dda64b0c85c3c5dd7b83822176cc3c710cd2b9 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/leockl
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@97dda64b0c85c3c5dd7b83822176cc3c710cd2b9 -
Trigger Event:
release
-
Statement type:
File details
Details for the file vidspy-0.1.2-py3-none-any.whl.
File metadata
- Download URL: vidspy-0.1.2-py3-none-any.whl
- Upload date:
- Size: 40.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
edea668d2487fa208956ea66316f52926e3902f3e514dbe77d23e730c8c9c383
|
|
| MD5 |
5245ccff2cdb329df25bfe8b6bc253ea
|
|
| BLAKE2b-256 |
6adb3854e82b36e6812c21cbc678e5ec5d2e0a1be5e2cf8703412ba0534644d5
|
Provenance
The following attestation bundles were made for vidspy-0.1.2-py3-none-any.whl:
Publisher:
publish.yml on leockl/vidspy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
vidspy-0.1.2-py3-none-any.whl -
Subject digest:
edea668d2487fa208956ea66316f52926e3902f3e514dbe77d23e730c8c9c383 - Sigstore transparency entry: 854918709
- Sigstore integration time:
-
Permalink:
leockl/vidspy@97dda64b0c85c3c5dd7b83822176cc3c710cd2b9 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/leockl
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@97dda64b0c85c3c5dd7b83822176cc3c710cd2b9 -
Trigger Event:
release
-
Statement type: