Python
3.12 or higher
This guide covers everything you need to install and configure LangStruct for your projects.
Python
3.12 or higher
Operating System
Linux, macOS, or Windows
Memory
2GB RAM minimum (4GB+ recommended)
API Access
Any LLM provider: Google, OpenAI, Anthropic, Azure, or local servers
Install LangStruct from PyPI using your preferred tooling:
uv add langstruct
pip install langstruct
Available extras map to features in this repo:
viz
— HTML visualization helpersexamples
— Example integrations (ChromaDB, LangChain)parallel
— tqdm for progress bars in batch runsdev
— test/lint/type tooling for contributorsall
— installs everything abovepip install "langstruct[viz]"pip install "langstruct[examples]"pip install "langstruct[parallel]"pip install "langstruct[dev]"pip install "langstruct[all]"
LangStruct supports hundreds of models via DSPy and LiteLLM. Examples include:
Set up any provider you prefer:
export GOOGLE_API_KEY="your-google-api-key"
Use any Gemini model: gemini-2.5-flash-lite
, gemini-2.5-flash
, gemini-2.5-pro
, etc.
export OPENAI_API_KEY="your-openai-api-key"
Use any OpenAI model: gpt-5-pro
, gpt-5-mini
, gpt-4o
, etc.
export ANTHROPIC_API_KEY="your-anthropic-api-key"
Use any Claude model: claude-opus-4-1
, claude-sonnet-4-0
, claude-3-7-sonnet-latest
, claude-3-5-haiku-latest
, etc.
export AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/"export AZURE_OPENAI_API_KEY="your-azure-api-key"export AZURE_OPENAI_API_VERSION="2024-02-01"
LangStruct works with local models via:
# Install Ollamacurl -fsSL https://ollama.ai/install.sh | sh
# Pull a modelollama pull llama2
# Use in LangStructexport OLLAMA_BASE_URL="http://localhost:11434"
# Install vLLMpip install vllm
# Start vLLM serverpython -m vllm.entrypoints.openai.api_server \ --model microsoft/DialoGPT-medium \ --port 8000
# Choose your preferred providerexport GOOGLE_API_KEY="your-google-api-key" # Google Geminiexport OPENAI_API_KEY="sk-..." # OpenAIexport OPENAI_ORG_ID="org-..." # Optionalexport ANTHROPIC_API_KEY="sk-ant-..." # Anthropic Claude
# LangStruct configurationexport LANGSTRUCT_DEFAULT_MODEL="your-preferred-model"export LANGSTRUCT_CACHE_DIR="~/.langstruct"export LANGSTRUCT_LOG_LEVEL="INFO"
Create a .env
file in your project root:
# Add your preferred provider's API keyGOOGLE_API_KEY=your-google-api-keyOPENAI_API_KEY=sk-your-key-hereANTHROPIC_API_KEY=sk-ant-your-key-hereLANGSTRUCT_DEFAULT_MODEL=your-preferred-model
Test your installation:
import langstruct
# Check versionprint(f"LangStruct version: {langstruct.__version__}")
# Test basic functionalityfrom pydantic import BaseModel, Fieldfrom langstruct import LangStruct
class TestSchema(BaseModel): message: str = Field(description="A simple message")
# This will test your API connection (uses your default model)extractor = LangStruct(schema=TestSchema)result = extractor.extract("Hello, LangStruct!")
print(f"Success! Extracted: {result.entities}")
# If you get import errors, reinstall with dependenciespip uninstall langstructpip install langstruct[all]
# Test your Google API keyfrom google import genaiclient = genai.Client() # Uses GOOGLE_API_KEYresponse = client.models.list()print("Google Gemini connection successful")
# Or test OpenAIimport openaiclient = openai.OpenAI() # Uses OPENAI_API_KEYresponse = client.models.list()print("OpenAI connection successful")
# Use user installation if neededpip install --user langstruct
# Or use virtual environment (recommended)python -m venv langstruct-envsource langstruct-env/bin/activate # On Windows: langstruct-env\Scripts\activatepip install langstruct
max_workers
for concurrency and rate_limit
to respect provider quotasshow_progress=True
(requires langstruct[parallel]
) for batch operationsrefine=True
) when accuracy matters most (higher cost)For contributing or development:
# Clone repositorygit clone https://github.com/langstruct/langstruct.gitcd langstruct
# Install in development modeuv sync --dev
# Or with pippip install -e ".[dev,test]"
# Run testspytest
# Run lintingruff check .mypy src/
Use any Python base image and install from PyPI or from source control. Ensure API keys are provided via environment variables.
Now that LangStruct is installed:
Quick Start
Basic Usage
Examples
API Reference
Need help? Check our troubleshooting guide or ask in GitHub Discussions.