| name | traigent-integrations |
| description | Integrate Traigent with LangChain, LiteLLM, DSPy, and other AI frameworks. Use when importing langchain/litellm/dspy alongside traigent, setting up multi-provider model testing, using auto_override_frameworks, or asking about framework-specific adapter patterns. |
| license | AGPL-3.0-only OR LicenseRef-Traigent-Commercial |
| metadata | {"author":"Traigent","version":"1.0"} |
Traigent Framework Integrations
When to Use
Use this skill when:
- Combining Traigent optimization with LangChain, LiteLLM, or DSPy
- Setting up multi-provider model testing (e.g., OpenAI + Anthropic + Google)
- Using
auto_override_frameworks or framework_targets in the decorator
- Writing optimized functions that call framework-specific APIs
- Connecting Traigent results to observability tools (MLflow, Weights & Biases)
Installation
Install Traigent with framework integration support:
pip install traigent[integrations]
pip install traigent langchain-openai langchain-anthropic
pip install traigent litellm
pip install traigent dspy
LangChain Integration
Traigent integrates with LangChain by optimizing the model and parameters used inside your chain. The key pattern: get the config from Traigent, then construct your LangChain objects.
Basic Pattern
import traigent
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
@traigent.optimize(
configuration_space={
"model": ["gpt-4o-mini", "gpt-4o"],
"temperature": [0.0, 0.3, 0.7, 1.0],
},
objectives=["accuracy"],
eval_dataset="questions.jsonl",
max_trials=10,
)
def answer_question(question):
config = traigent.get_config()
llm = ChatOpenAI(
model=config["model"],
temperature=config["temperature"],
)
prompt = ChatPromptTemplate.from_messages([
("system", "Answer the question accurately and concisely."),
("human", "{question}"),
])
chain = prompt | llm
response = chain.invoke({"question": question})
return response.content
results = answer_question.optimize()
Auto Override Frameworks
The auto_override_frameworks flag lets Traigent automatically intercept LangChain model instantiation to inject the optimized configuration. This is useful when you want Traigent to manage model selection across your chain without manually threading get_config():
@traigent.optimize(
configuration_space={
"model": ["gpt-4o-mini", "gpt-4o", "claude-3-haiku-20240307"],
"temperature": [0.0, 0.5, 1.0],
},
objectives=["accuracy"],
max_trials=12,
auto_override_frameworks=True,
framework_targets=["langchain_openai.ChatOpenAI"],
)
def summarize_document(text):
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.5)
response = llm.invoke(text)
return response.content
Note: auto_override_frameworks=True alone is a no-op — you must also set framework_targets to specify which classes to intercept. The flag without targets is silently ignored.
For finer control, use framework_targets to specify exactly which classes to override:
@traigent.optimize(
configuration_space={
"model": ["gpt-4o-mini", "gpt-4o"],
"temperature": [0.0, 0.5],
},
objectives=["accuracy"],
max_trials=8,
framework_targets=["langchain_openai.ChatOpenAI"],
)
def my_chain(input_text):
llm = ChatOpenAI(model="gpt-4o-mini")
return llm.invoke(input_text).content
See LangChain reference for RAG chain optimization and advanced patterns.
LiteLLM Multi-Provider
LiteLLM provides a unified completion() interface across 100+ LLM providers. This makes it natural to optimize across providers with Traigent:
import traigent
import litellm
@traigent.optimize(
configuration_space={
"model": [
"gpt-4o-mini",
"gpt-4o",
"claude-3-haiku-20240307",
"claude-3-5-sonnet-20241022",
"gemini/gemini-1.5-flash",
],
"temperature": [0.0, 0.3, 0.7],
"max_tokens": [256, 512, 1024],
},
objectives=["accuracy"],
eval_dataset="classification_eval.jsonl",
max_trials=15,
)
def classify_text(text):
config = traigent.get_config()
response = litellm.completion(
model=config["model"],
messages=[{"role": "user", "content": f"Classify this text: {text}"}],
temperature=config["temperature"],
max_tokens=config["max_tokens"],
)
return response.choices[0].message.content
results = classify_text.optimize()
for trial in results.successful_trials:
model = trial.config["model"]
accuracy = trial.get_metric("accuracy", 0.0)
print(f"{model}: accuracy={accuracy:.2%}")
LiteLLM handles API key routing automatically based on the model prefix. Set provider API keys in environment variables:
export OPENAI_API_KEY="sk-..."
export ANTHROPIC_API_KEY="sk-ant-..."
export GEMINI_API_KEY="..."
See LiteLLM reference for the full provider list and cost tracking details.
DSPy Integration
Traigent provides a DSPyPromptOptimizer adapter that wraps DSPy's MIPROv2 and BootstrapFewShot optimizers for automatic prompt engineering:
from traigent.integrations.dspy_adapter import DSPyPromptOptimizer
optimizer = DSPyPromptOptimizer(method="mipro")
result = optimizer.optimize_prompt(
module=my_dspy_module,
trainset=train_examples,
metric=accuracy_metric,
)
optimized_module = result.optimized_module
print(f"Best score: {result.best_score}")
print(f"Method: {result.method}")
print(f"Demos: {result.num_demos}")
You can also use DSPy modules inside a Traigent-optimized function for model-level optimization:
import traigent
import dspy
@traigent.optimize(
configuration_space={
"model": ["gpt-4o-mini", "gpt-4o"],
"temperature": [0.0, 0.5, 1.0],
},
objectives=["accuracy"],
max_trials=6,
)
def dspy_qa(question):
config = traigent.get_config()
lm = dspy.LM(config["model"], temperature=config["temperature"])
dspy.configure(lm=lm)
qa = dspy.Predict("question -> answer")
result = qa(question=question)
return result.answer
See DSPy reference for BootstrapFewShot patterns and advanced configuration.
Observability Integrations
MLflow and Weights & Biases are not included in the base traigent package. Install them separately:
pip install mlflow
pip install wandb
MLflow
Log Traigent optimization results to MLflow for experiment tracking:
import mlflow
results = func.optimize()
with mlflow.start_run():
mlflow.log_param("algorithm", results.algorithm)
mlflow.log_param("best_config", results.best_config)
mlflow.log_metric("best_score", results.best_score or 0.0)
mlflow.log_metric("total_cost", results.total_cost or 0.0)
mlflow.log_metric("total_trials", len(results.trials))
mlflow.log_metric("success_rate", results.success_rate)
for trial in results.successful_trials:
with mlflow.start_run(nested=True, run_name=trial.trial_id):
mlflow.log_params(trial.config)
mlflow.log_metrics(trial.metrics)
Weights & Biases
import wandb
results = func.optimize()
wandb.init(project="traigent-optimization")
for trial in results.trials:
wandb.log({
"trial_id": trial.trial_id,
"status": str(trial.status),
**trial.config,
**trial.metrics,
})
wandb.log({
"best_score": results.best_score,
"best_config": results.best_config,
"total_cost": results.total_cost,
})
wandb.finish()
Pattern: The Right Way
When using Traigent with any framework, always follow this order:
- Get the config from Traigent
- Create framework objects using that config
- Execute with those objects
@traigent.optimize(
configuration_space={"model": ["gpt-4o-mini", "gpt-4o"], "temperature": [0.0, 0.5]},
objectives=["accuracy"],
)
def my_func(text):
config = traigent.get_config()
llm = ChatOpenAI(
model=config["model"],
temperature=config["temperature"],
)
return llm.invoke(text).content
Do not create the client outside the function or before getting the config:
llm = ChatOpenAI(model="gpt-4o-mini")
@traigent.optimize(...)
def my_func(text):
return llm.invoke(text).content
The exception is when using auto_override_frameworks=True, which intercepts client construction automatically.
Reference Files