BRAID (Bounded Reasoning for Autonomous Inference and Decisions) integration for DSPy framework
Project description
BRAID-DSPy Integration
A Python library that integrates BRAID (Bounded Reasoning for Autonomous Inference and Decisions) architecture into the DSPy framework, enabling structured reasoning through Guided Reasoning Diagrams (GRD) in Mermaid format.
Overview
BRAID-DSPy brings structured reasoning capabilities to DSPy by requiring models to first generate a machine-readable flowchart (GRD) before executing the solution. This separation of planning and execution significantly improves reliability and reduces hallucinations.
Motivation
This project began when I first encountered the BRAID architecture during one of Armağan Amcalar's live streams. The two-phase reasoning approach — planning first, then execution — and the idea of representing this planning in a visualizable format (Mermaid diagrams) immediately captured my interest.
After the stream, I delved into OpenServ's articles and technical details about BRAID. The approach of having the model first generate a flowchart (Guided Reasoning Diagram - GRD) and then execute the solution step-by-step according to this schema seemed like a significant step forward for reliability and transparency in AI systems. I realized that integrating this architecture with the DSPy framework would need to work seamlessly with existing DSPy modules and optimizers, which led me to develop this library to make that integration a reality.
Much of the development process involved "vibe coding" — following intuition and iterating based on what felt right rather than strictly following a predefined plan. This organic approach allowed the library to evolve naturally as I explored the integration between BRAID and DSPy.
Key Features
- Guided Reasoning Diagrams (GRD): Generate Mermaid-format flowcharts that map solution steps
- Two-Phase Reasoning: Separate planning and execution phases for better reliability
- DSPy Integration: Seamlessly integrates with existing DSPy modules and optimizers
- Auditable Reasoning: Visualize and debug reasoning processes through GRD diagrams
- Optimization Support: BRAID-aware optimizers for improving GRD quality
Installation
pip install braid-dspy
Quick Start
import dspy
from braid import BraidReasoning
# Configure DSPy
lm = dspy.OpenAI(model="gpt-4")
dspy.configure(lm=lm)
# Create a BRAID reasoning module
braid = BraidReasoning()
# Use it in your pipeline
result = braid(problem="Solve: If a train travels 120 km in 2 hours, what is its speed?")
print(result.answer)
print(result.grd) # View the reasoning diagram
Architecture
BRAID-DSPy implements a two-phase reasoning architecture that separates planning from execution, significantly improving reliability and reducing hallucinations compared to traditional Chain-of-Thought approaches.
High-Level Architecture
flowchart TD
A[Problem Input] --> B[Planning Phase]
B --> C[GRD Generation]
C --> D[Mermaid Diagram]
D --> E[Parsing Phase]
E --> F[GRD Structure]
F --> G[Execution Phase]
G --> H[Step-by-Step Execution]
H --> I[Final Answer]
B --> B1["GRDGenerator<br/>or<br/>Direct LLM Call"]
E --> E1["MermaidParser<br/>Validates & Parses"]
G --> G1["Execute Each Node<br/>in Order"]
G1 --> G2["Build Context<br/>from Previous Steps"]
G2 --> G3["LLM Execution<br/>per Step"]
classDef planningPhase fill:#e1f5ff,stroke:#01579b,stroke-width:2px
classDef parsingPhase fill:#fff4e1,stroke:#e65100,stroke-width:2px
classDef executionPhase fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px
class B,B1,C planningPhase
class E,E1,F parsingPhase
class G,G1,G2,G3,H executionPhase
Two-Phase Reasoning Process
Phase 1: Planning
In the planning phase, the system generates a Guided Reasoning Diagram (GRD) in Mermaid format that maps out the solution steps:
flowchart LR
A[Problem] --> B[GRDGenerator]
B --> C{Generation Method}
C -->|With Generator| D["Few-shot Examples<br/>+ Structured Prompt"]
C -->|Direct| E["DSPy Signature<br/>BraidPlanSignature"]
D --> F[Mermaid GRD]
E --> F
F --> G[Validation]
G -->|Valid| H[Parsed Structure]
G -->|Invalid| I[Error]
classDef generator fill:#e1f5ff,stroke:#01579b,stroke-width:2px
classDef grd fill:#fff4e1,stroke:#e65100,stroke-width:2px
classDef parsed fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px
class B,D,E generator
class F,G grd
class H parsed
Example GRD Output:
flowchart TD
Start[Problem Analysis] --> Identify[Identify given values:<br/>Distance = 120 km<br/>Time = 2 hours]
Identify --> Formula[Recall speed formula:<br/>Speed = Distance / Time]
Formula --> Calculate[Calculate:<br/>Speed = 120 / 2]
Calculate --> Answer[Speed = 60 km/h]
Phase 2: Execution
The execution phase follows the GRD structure step-by-step:
flowchart TD
A[Parsed GRD] --> B[Get Execution Order]
B --> C[For Each Node]
C --> D["Build Context<br/>Problem + Previous Steps"]
D --> E["Execute Step<br/>via LLM"]
E --> F[Store Result]
F --> G{More Steps?}
G -->|Yes| C
G -->|No| H[Extract Final Answer]
H --> I[Return BraidResult]
classDef loop fill:#e1f5ff,stroke:#01579b,stroke-width:2px
classDef execution fill:#fff4e1,stroke:#e65100,stroke-width:2px
classDef result fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px
class C,D loop
class E,F execution
class H,I result
Component Architecture
graph TB
subgraph BRAID["BRAID-DSPy Components"]
A["BraidReasoning<br/>Main Module"]
B["GRDGenerator<br/>Planning"]
C["MermaidParser<br/>Parsing & Validation"]
D["BraidOptimizer<br/>Optimization"]
E["DSPy Signatures<br/>BraidPlanSignature<br/>BraidStepSignature"]
end
subgraph DSPY["DSPy Framework"]
F[DSPy Modules]
G[LLM Backend]
H[Optimizers]
end
A --> B
A --> C
A --> E
A --> D
B --> F
E --> F
D --> H
F --> G
classDef main fill:#4a90e2,stroke:#01579b,stroke-width:3px,color:#fff
classDef planning fill:#e1f5ff,stroke:#01579b,stroke-width:2px
classDef parsing fill:#fff4e1,stroke:#e65100,stroke-width:2px
classDef optimization fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px
class A main
class B,E planning
class C parsing
class D optimization
Key Components
-
BraidReasoning: Main module that orchestrates the two-phase process
- Manages planning and execution phases
- Handles GRD generation or accepts pre-generated GRDs
- Executes steps sequentially with context accumulation
-
GRDGenerator: Generates Mermaid-formatted GRDs using few-shot examples
- Uses structured prompts with examples
- Supports retry logic for robust generation
- Extracts Mermaid code from LLM responses
-
MermaidParser: Parses and validates Mermaid diagrams
- Converts Mermaid syntax to structured
GRDStructureobjects - Validates diagram syntax and structure
- Determines execution order using topological sorting
- Converts Mermaid syntax to structured
-
BraidOptimizer: Optimizes both planning and execution phases
- Can optimize GRD generation quality
- Improves step execution through DSPy optimizers
- Supports custom metrics for evaluation
Execution Flow Example
For a problem like "If a train travels 120 km in 2 hours, what is its speed?":
sequenceDiagram
participant User
participant BraidReasoning
participant GRDGenerator
participant MermaidParser
participant LLM
User->>BraidReasoning: problem="..."
BraidReasoning->>GRDGenerator: generate(problem)
GRDGenerator->>LLM: Generate GRD with examples
LLM-->>GRDGenerator: Mermaid diagram
GRDGenerator-->>BraidReasoning: GRD string
BraidReasoning->>MermaidParser: parse(grd)
MermaidParser->>MermaidParser: Validate syntax
MermaidParser->>MermaidParser: Extract nodes & edges
MermaidParser->>MermaidParser: Determine execution order
MermaidParser-->>BraidReasoning: GRDStructure
loop For each step in execution order
BraidReasoning->>BraidReasoning: Build context
BraidReasoning->>LLM: Execute step
LLM-->>BraidReasoning: Step result
BraidReasoning->>BraidReasoning: Store result
end
BraidReasoning->>BraidReasoning: Extract final answer
BraidReasoning-->>User: BraidResult(answer, grd, steps)
Benefits of This Architecture
- Reliability: Planning phase ensures structured approach before execution
- Transparency: GRD diagrams provide visual reasoning trace
- Debuggability: Each step is isolated and traceable
- Optimization: Both phases can be optimized independently
- Flexibility: Supports pre-generated GRDs or dynamic generation
Documentation
📚 Full documentation is available on Read the Docs
Local documentation:
To build documentation locally:
pip install -e ".[docs]"
cd docs
make html
Examples
Check out the examples directory for:
- Basic usage examples
- GSM8K benchmark integration
- Optimization workflows
Contributing
Contributions are welcome! Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.
Changelog
See CHANGELOG.md for a list of changes and version history.
License
MIT License - see LICENSE file for details.
References
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 braid_dspy-0.1.6.tar.gz.
File metadata
- Download URL: braid_dspy-0.1.6.tar.gz
- Upload date:
- Size: 31.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea62cf0bc07fa0f714c2a9f680eab2c53cab00f5c74a5d2a0dc9fbdf044f1787
|
|
| MD5 |
477443618bdf77cf7760731df8e76259
|
|
| BLAKE2b-256 |
6a663ddcfaea0d7aec624efdc16df24d9ff50ad7d02da05e74433062ab8d09c9
|
File details
Details for the file braid_dspy-0.1.6-py3-none-any.whl.
File metadata
- Download URL: braid_dspy-0.1.6-py3-none-any.whl
- Upload date:
- Size: 22.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20bced23a0ef2cfb050af8bddfd069a1bfa1e97f5f173830effec1a282e8f1e5
|
|
| MD5 |
bc77e8ee3107d553bb9118f65ebe7f7a
|
|
| BLAKE2b-256 |
95297fed052ee1230a26d1039b6fd0720499ce7d0631f94bf105169471287830
|