# stanfordnlp/dspy

**Attribution required: if you use, quote, or summarise this content, you must credit and link back to [awesome-repositories.com](https://awesome-repositories.com/repository/stanfordnlp-dspy).**

32,291 stars · 2,632 forks · Python · mit

## Links

- GitHub: https://github.com/stanfordnlp/dspy
- Homepage: https://dspy.ai
- awesome-repositories: https://awesome-repositories.com/repository/stanfordnlp-dspy.md

## Description

DSPy is a declarative programming framework designed for building complex language model applications. It treats model interactions as modular, composable programs, allowing developers to define task logic through typed class schemas rather than relying on manually written prompts. By organizing workflows into hierarchical, reusable Python objects, the framework enables the construction of sophisticated AI systems that manage state and execution flow independently.

The framework distinguishes itself through an automated optimization engine that iteratively refines prompt instructions and few-shot demonstrations. By evaluating candidate programs against defined metrics and feedback loops, it systematically improves performance without requiring manual prompt engineering. This process is supported by a programmatic evaluation harness that measures output quality using custom metrics and model-based judges, ensuring consistent behavior across multi-stage pipelines.

Beyond core orchestration, the system provides a robust interface for structured data extraction and tool integration. It includes mechanisms for wrapping Python functions as tools, executing iterative reasoning loops, and adapting model outputs into validated data structures. These capabilities are complemented by comprehensive state management and persistence utilities, which allow for the versioning and tracking of program configurations throughout the development lifecycle.

## Tags

### Artificial Intelligence & ML

- [Declarative AI Frameworks](https://awesome-repositories.com/f/artificial-intelligence-ml/declarative-ai-frameworks.md) — Specifies desired model behaviors using high-level declarations that translate intent into executable instructions. ([source](https://dspy.ai/getting-started/first-program/index.md))
- [Agentic Orchestration Frameworks](https://awesome-repositories.com/f/artificial-intelligence-ml/agentic-orchestration-frameworks.md) — Organizes complex AI workflows into nested, reusable objects that manage state and execution flow independently.
- [AI Signature Definitions](https://awesome-repositories.com/f/artificial-intelligence-ml/ai-signature-definitions.md) — Creates signatures using classes and docstrings to provide detailed instructional context. ([source](https://dspy.ai/getting-started/class-based-signatures/index.md))
- [Automated Prompt Engineering](https://awesome-repositories.com/f/artificial-intelligence-ml/automated-prompt-engineering.md) — Improves program instructions by using a reflection loop that evaluates candidates and proposes iterative improvements. ([source](https://dspy.ai/diving-deeper/gepa-in-depth/index.md))
- [Automated Prompt Optimization](https://awesome-repositories.com/f/artificial-intelligence-ml/automated-prompt-optimization.md) — Systematically improves model performance by iteratively refining instructions and few-shot examples using evaluation metrics.
- [Declarative AI Programming](https://awesome-repositories.com/f/artificial-intelligence-ml/declarative-ai-programming.md) — Defines task logic through modular, typed schemas to build complex language model applications.
- [Declarative Task Signatures](https://awesome-repositories.com/f/artificial-intelligence-ml/declarative-task-signatures.md) — Defines task logic and data flow requirements through declarative, typed class schemas. ([source](https://dspy.ai/getting-started/expanding-signatures/index.md))
- [Agentic Orchestration Modules](https://awesome-repositories.com/f/artificial-intelligence-ml/agentic-orchestration-modules.md) — ## `dspy.Module(callbacks=None)` Bases: `BaseModule` Base class for all DSPy modules (programs). A Module is a building block for DSPy programs that can contain predictors, sub-modules, and custom logic. Modules can be c ([source](https://dspy.ai/api/modules/Module/index.md))
- [Agentic Reasoning Loops](https://awesome-repositories.com/f/artificial-intelligence-ml/agentic-reasoning-loops.md) — Executes iterative reasoning loops that allow models to synthesize information and call tools until a task is resolved. ([source](https://dspy.ai/getting-started/react-and-tools/index.md))
- [Few-Shot Learning Optimizers](https://awesome-repositories.com/f/artificial-intelligence-ml/few-shot-learning-optimizers.md) — Selects the best set of few-shot demonstrations by running multiple bootstrap rounds and evaluating performance. ([source](https://dspy.ai/diving-deeper/bootstrap-fewshot-family/index.md))
- [Iterative Program Optimization](https://awesome-repositories.com/f/artificial-intelligence-ml/iterative-program-optimization.md) — Improves task reasoning and execution efficiency by applying prompt and weight adjustments in sequence. ([source](https://dspy.ai/api/optimizers/BetterTogether/index.md))
- [Optimization Engines](https://awesome-repositories.com/f/artificial-intelligence-ml/optimization-engines.md) — Provides a computational layer that iteratively refines instructions and demonstrations based on feedback.
- [Agentic Workflow Orchestration](https://awesome-repositories.com/f/artificial-intelligence-ml/agentic-workflow-orchestration.md) — Constructs autonomous reasoning loops that integrate language models with external tools and APIs. ([source](https://dspy.ai/diving-deeper/modules/index.md))
- [Composable Agentic Architectures](https://awesome-repositories.com/f/artificial-intelligence-ml/composable-agentic-architectures.md) — Enables the construction of complex reasoning loops through hierarchical, reusable Python objects.
- [Predictor Modules](https://awesome-repositories.com/f/artificial-intelligence-ml/predictor-modules.md) — Maps inputs to outputs using a language model as the fundamental prediction unit. ([source](https://dspy.ai/api/modules/Predict/index.md))
- [Prompt Optimization Strategies](https://awesome-repositories.com/f/artificial-intelligence-ml/prompt-optimization-strategies.md) — Improves prediction quality by generating feedback on failed attempts and injecting that advice into subsequent runs. ([source](https://dspy.ai/diving-deeper/built-in-module-variants/index.md))
- [Signature Interfaces](https://awesome-repositories.com/f/artificial-intelligence-ml/signature-interfaces.md) — Translates task signatures into model-specific prompts and parses responses into structured types for consistent communication. ([source](https://dspy.ai/diving-deeper/adapters/index.md))
- [Structured Data Coercion](https://awesome-repositories.com/f/artificial-intelligence-ml/structured-data-coercion.md) — Automatically coerces and validates language model outputs into structured, typed data formats. ([source](https://dspy.ai/getting-started/expanding-signatures/index.md))
- [Chain-of-Thought Modules](https://awesome-repositories.com/f/artificial-intelligence-ml/chain-of-thought-modules.md) — Implements modules that reason step by step to improve performance on complex tasks. ([source](https://dspy.ai/api/modules/ChainOfThought/index.md))
- [Demonstration Bootstrapping](https://awesome-repositories.com/f/artificial-intelligence-ml/demonstration-bootstrapping.md) — Creates few-shot examples by running a teacher model on training data and filtering successful traces. ([source](https://dspy.ai/diving-deeper/bootstrap-fewshot-family/index.md))
- [Few-Shot Optimizers](https://awesome-repositories.com/f/artificial-intelligence-ml/few-shot-optimizers.md) — Automatically selects and filters successful traces to populate demonstration slots for improved model performance.
- [Modular AI Pipelines](https://awesome-repositories.com/f/artificial-intelligence-ml/modular-ai-pipelines.md) — Assembles sophisticated AI systems from reusable, hierarchical components that manage state and execution flow.
- [Module Composition](https://awesome-repositories.com/f/artificial-intelligence-ml/module-composition.md) — Builds complex logic by subclassing base modules and piping inputs through sub-modules. ([source](https://dspy.ai/getting-started/composing-modules/index.md))
- [Tool Integration Interfaces](https://awesome-repositories.com/f/artificial-intelligence-ml/tool-integration-interfaces.md) — Wraps Python functions as tools that language models can invoke to perform specific tasks. ([source](https://dspy.ai/getting-started/react-and-tools/index.md))
- [Tool-Using Agents](https://awesome-repositories.com/f/artificial-intelligence-ml/tool-using-agents.md) — Enables language models to interact with external tools and functions through reasoning and acting loops. ([source](https://dspy.ai/api/modules/ReAct/index.md))
- [Code Execution Agents](https://awesome-repositories.com/f/artificial-intelligence-ml/code-execution-agents.md) — Utilizes code interpretation to solve problems through iterative reasoning and execution loops. ([source](https://dspy.ai/api/modules/CodeAct/index.md))
- [Dynamic Demonstration Retrieval](https://awesome-repositories.com/f/artificial-intelligence-ml/dynamic-demonstration-retrieval.md) — Populates predictor demo slots dynamically at inference time using K-nearest neighbors. ([source](https://dspy.ai/diving-deeper/bootstrap-fewshot-family/index.md))
- [Inference Sampling Strategies](https://awesome-repositories.com/f/artificial-intelligence-ml/inference-sampling-strategies.md) — Generates multiple results and selects the best candidate based on custom reward functions. ([source](https://dspy.ai/diving-deeper/built-in-module-variants/index.md))
- [Program-of-Thought Modules](https://awesome-repositories.com/f/artificial-intelligence-ml/program-of-thought-modules.md) — Solves problems by generating and executing Python programs within the reasoning loop. ([source](https://dspy.ai/api/modules/ProgramOfThought/index.md))
- [Structured Data Extraction](https://awesome-repositories.com/f/artificial-intelligence-ml/structured-data-extraction.md) — Converts unstructured model outputs into validated, typed data structures for reliable software integration.
- [Structured Data Parsers](https://awesome-repositories.com/f/artificial-intelligence-ml/structured-data-parsers.md) — Enforces structured output formats by mapping model completions to defined Python types and validating them.
- [Structured Model Interfaces](https://awesome-repositories.com/f/artificial-intelligence-ml/structured-model-interfaces.md) — Maps model inputs and outputs onto validated data structures to ensure consistent communication.
- [Ensemble Modules](https://awesome-repositories.com/f/artificial-intelligence-ml/ensemble-modules.md) — Runs modules multiple times with different rollouts to select the best output based on reward functions. ([source](https://dspy.ai/api/modules/BestOfN/index.md))
- [Input Validation Schemas](https://awesome-repositories.com/f/artificial-intelligence-ml/input-validation-schemas.md) — Enforces strict field constraints and data consistency within language model interactions using typed schemas. ([source](https://dspy.ai/getting-started/class-based-signatures/index.md))
- [Model Context Configuration](https://awesome-repositories.com/f/artificial-intelligence-ml/model-context-configuration.md) — Assigns specific language models to different subtasks to ensure optimal model usage. ([source](https://dspy.ai/getting-started/composing-modules/index.md))
- [Prompt Adapters](https://awesome-repositories.com/f/artificial-intelligence-ml/prompt-adapters.md) — Provides automated transformation of inputs into structured prompts and parses model responses into consistent data formats. ([source](https://dspy.ai/api/adapters/Adapter/index.md))
- [Refinement Modules](https://awesome-repositories.com/f/artificial-intelligence-ml/refinement-modules.md) — Iteratively refines module outputs by running them multiple times against reward functions. ([source](https://dspy.ai/api/modules/Refine/index.md))

### Software Engineering & Architecture

- [Modular Program Composition](https://awesome-repositories.com/f/software-engineering-architecture/modular-program-composition.md) — Assembles complex software systems by combining independent, reusable functional units. ([source](https://dspy.ai/getting-started/first-program/index.md))

### Testing & Quality Assurance

- [LLM Evaluation](https://awesome-repositories.com/f/testing-quality-assurance/model-testing/llm-evaluation.md) — Measures output quality using custom metrics and model-based judges to ensure consistent behavior across pipelines.
- [Formal Verification Tools](https://awesome-repositories.com/f/testing-quality-assurance/validation-verification/formal-verification-tools.md) — Validates data structures and function signatures at runtime to ensure consistent communication. ([source](https://dspy.ai/getting-started/first-program/index.md))
- [Model Evaluation](https://awesome-repositories.com/f/testing-quality-assurance/model-testing/model-evaluation.md) — Enables the creation of custom functions to measure program success and guide optimization. ([source](https://dspy.ai/diving-deeper/metrics-and-evaluation/index.md))

### Data & Databases

- [Model State Persistence](https://awesome-repositories.com/f/data-databases/model-state-persistence.md) — Saves optimized program states or module structures to disk for portability. ([source](https://dspy.ai/getting-started/saving-and-loading/index.md))
- [Model State Restoration](https://awesome-repositories.com/f/data-databases/model-state-restoration.md) — Restores optimized instructions and metadata by loading saved program states back into class instances. ([source](https://dspy.ai/getting-started/saving-and-loading/index.md))
