DSPy with a custom OpenAI endpoint: dspy.LM setup
Point dspy.LM at an OpenAI-compatible base URL with an openai/ prefix, test one call before an optimizer run, and use a second catalog model on the same key.
DSPy talks to UplinkSwitch with one constructor call: dspy.LM("openai/gpt-6.1-sol", api_base="https://api.uplinkswitch.com/v1", api_key=...). The openai/ prefix is DSPy's instruction to speak OpenAI Chat Completions with bearer authentication; the gateway's model id is only the part after the slash.
One call before any optimizer
import os
import dspy
lm = dspy.LM(
"openai/gpt-6.1-sol",
api_base="https://api.uplinkswitch.com/v1",
api_key=os.environ["UPLINKSWITCH_API_KEY"],
cache=False, # a cached reply would not prove the endpoint was reached
)
dspy.configure(lm=lm)
print(lm("Reply with the single word: ready"))This follows the "Other providers" recipe in DSPy's language-model docs as read on 2026-11-22 (an openai/ prefix with an explicit API base and key). It was checked against those docs, not executed for this page. DSPy caches replies by default, so the check turns the cache off; remove that line afterwards. The direct call proves the connection. A module adds DSPy's adapter on top, so test one separately:
qa = dspy.Predict("question -> answer")
print(qa(question="What does a reranker do?").answer)Run that single prediction first. An optimization job sends many requests per example, so a wrong base URL or id is cheaper to find here than fifty calls into a compile.
The prefix is not part of the id
| You write in DSPy | The gateway receives |
|---|---|
openai/gpt-6.1-sol | gpt-6.1-sol |
openai/claude-sonnet-5-5 | claude-sonnet-5-5 |
Claude ids keep the openai/ prefix here because the prefix selects the wire dialect, not the developer. Writing anthropic/claude-sonnet-5-5 would make DSPy send Anthropic's format to a different default host. DSPy's docs on that date describe a native engine with LiteLLM as an optional compatibility route; if your install is an older, LiteLLM-backed release, the LiteLLM guide covers that adapter's own settings.
A second model on the same key
strong = dspy.LM(
"openai/claude-sonnet-5-5",
api_base="https://api.uplinkswitch.com/v1",
api_key=os.environ["UPLINKSWITCH_API_KEY"],
)
with dspy.context(lm=strong):
print(qa(question="Summarise the trade-off between recall and precision.").answer)| Catalog id after the prefix | Input / output, per 1M tokens |
|---|---|
gpt-6.1-sol | $0.582 / $2.91 |
claude-sonnet-5-5 | $1.164 / $2.91 |
No claim is made here that every optimizer, adapter or structured-output mode behaves the same on every id. Test the ones your program uses.