DSPy
DSPy programs with an OpenAI-compatible uncensored API
Configure dspy.LM for Abliterator with the openai/ prefix, api_base and model_type="chat", stream output fields with streamify, and score programs with dspy.Evaluate.
Use dspy.LM("openai/kimi-k3.1-pro-abliterated", api_base="https://abliterator.ai/v1", api_key=..., model_type="chat") and dspy.configure(lm=lm). Without model_type="chat", DSPy 3.5 sends openai/ models to the Responses API.
DSPy describes LMs as provider/model strings. For an OpenAI-compatible endpoint the DSPy setup guide uses the openai/ prefix plus api_base and api_key. With current DSPy you also need model_type="chat". Tested with dspy 3.5.
Install
pip install dspy
export ABL_API_KEY="sk-abl-v1-..."
Configure the LM
import os
import dspy
lm = dspy.LM(
"openai/kimi-k3.1-pro-abliterated",
api_base="https://abliterator.ai/v1",
api_key=os.environ["ABL_API_KEY"],
model_type="chat",
max_tokens=4000,
)
dspy.configure(lm=lm)
class ExplainFinding(dspy.Signature):
"""Explain a security finding to the engineer who must fix it."""
finding: str = dspy.InputField()
explanation: str = dspy.OutputField()
fix: str = dspy.OutputField()
explain = dspy.ChainOfThought(ExplainFinding)
pred = explain(finding="Session cookie set without HttpOnly or Secure on login.example.com")
print(pred.reasoning)
print(pred.fix)
dspy.configure(lm=lm) sets the default for every module in the process. Use with dspy.context(lm=other_lm): to run one block on a different model, for example deepseek-v4.1-flash-abliterated for cheap bulk passes.
Stream an output field
dspy.streamify wraps a program so it yields chunks for the fields you listen to, then the final Prediction:
import asyncio
import os
import dspy
lm = dspy.LM(
"openai/deepseek-v4.1-flash-abliterated",
api_base="https://abliterator.ai/v1",
api_key=os.environ["ABL_API_KEY"],
model_type="chat",
cache=False,
)
dspy.configure(lm=lm)
program = dspy.ChainOfThought("question -> answer")
stream_program = dspy.streamify(
program,
stream_listeners=[dspy.streaming.StreamListener(signature_field_name="answer")],
)
async def main() -> None:
async for item in stream_program(question="What makes a good honeytoken, and where should one live?"):
if isinstance(item, dspy.streaming.StreamResponse):
print(item.chunk, end="", flush=True)
elif isinstance(item, dspy.Prediction):
print("\n\nfinal:", item.answer[:60])
asyncio.run(main())
Only fields with a listener stream; the reasoning field arrives in the final Prediction. The example sets cache=False so a repeated question streams a fresh answer instead of a cached one.
Score a program with dspy.Evaluate
DSPy programs are meant to be measured. This example checks a phishing classifier against a small labeled set, the kind of eval deepseek-v4.1-flash-abliterated makes cheap at $1.00 per million input tokens:
import os
from typing import Literal
import dspy
lm = dspy.LM(
"openai/deepseek-v4.1-flash-abliterated",
api_base="https://abliterator.ai/v1",
api_key=os.environ["ABL_API_KEY"],
model_type="chat",
)
dspy.configure(lm=lm)
class Classify(dspy.Signature):
"""Label an email for a trust-and-safety dataset."""
email: str = dspy.InputField()
label: Literal["phishing", "benign"] = dspy.OutputField()
devset = [
dspy.Example(email="Your payroll account is locked. Re-enter your SSN here within 2 hours.", label="phishing").with_inputs("email"),
dspy.Example(email="Reminder: the all-hands moves to 3pm Thursday, same room.", label="benign").with_inputs("email"),
dspy.Example(email="DocuSign: you have a pending invoice, log in with your Microsoft password to view.", label="phishing").with_inputs("email"),
]
def exact(example, pred, trace=None):
return example.label == pred.label
evaluate = dspy.Evaluate(devset=devset, metric=exact, num_threads=4, display_progress=False)
print(evaluate(dspy.Predict(Classify)))
Keep num_threads at or below your plan's concurrent-request limit (/pricing). Optimizers such as dspy.MIPROv2 make many calls, so start them on deepseek-v4.1-flash-abliterated. For tool-using agents, dspy.ReAct accepts plain Python functions as tools on the same LM.
Troubleshooting
LMInvalidRequestError: ... /v1/responses.model_typeis missing. Addmodel_type="chat".AdapterParseError: Expected to find output fields. The model's reply did not follow DSPy's field format. DSPy retries with a JSON adapter automatically; if it still fails, simplify the signature or raisemax_tokensso long outputs are not cut off.- Results never change between runs. DSPy caches identical requests. Pass
cache=Falsetodspy.LM, or vary the inputs. - 404 on the base URL.
api_basemust behttps://abliterator.ai/v1, including/v1. - Old tutorials use
dspy.OpenAI(...)ordspy.settings.configure. Those APIs were replaced bydspy.LManddspy.configure.
Frequently asked questions
Why does DSPy call /v1/responses?
In DSPy 3.5, model_type defaults to auto, and for the openai provider auto means the Responses API. Abliterator serves Chat Completions, so set model_type="chat" on every dspy.LM that points at Abliterator.
Do I need the dspy[litellm] extra?
No. The openai/ prefix with api_base runs on DSPy's built-in native engine. LiteLLM is only needed for providers the native engine does not cover.
Should I turn off DSPy's cache for evaluations?
Turn it off when you measure behavior that should vary between runs, such as refusal rates with temperature above zero. Leave it on while you iterate on prompts and optimizers; repeated identical calls are then free.