---
title: "DSPy (gateway)"
description: "Set up DSPy with the Respan gateway."
---

Route DSPy's underlying LLM calls through the Respan gateway to use 1000+ models from different providers. Only your `RESPAN_API_KEY` is needed; no separate provider keys required.

Paste this into your coding agent:

```text
Help me set up the Respan gateway with the DSPy integration.
Follow https://www.respan.ai/docs/integrations/gateway/ds-py.md
```

## Setup

<Steps>
<Step title="Install packages">

```bash
pip install dspy
```

</Step>

<Step title="Set environment variables">

```bash
export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
```

No `OPENAI_API_KEY` needed: the Respan gateway handles provider authentication.

</Step>

<Step title="Point DSPy to the Respan gateway">

```python
import os
import dspy

lm = dspy.LM(
    "openai/gpt-5-mini",
    api_key=os.environ["RESPAN_API_KEY"],
    api_base="https://gateway.respan.ai",
    cache=False,
)
dspy.configure(lm=lm)

class QA(dspy.Signature):
    question: str = dspy.InputField()
    answer: str = dspy.OutputField()

result = dspy.ChainOfThought(QA)(question="What is the capital of France?")
print(result.answer)
```

</Step>
</Steps>

## Switch models

Use another OpenAI model ID through the same gateway-backed LiteLLM route.

```python
import os
import dspy

gateway_kwargs = {
    "api_key": os.environ["RESPAN_API_KEY"],
    "api_base": "https://gateway.respan.ai",
    "cache": False,
}

dspy.configure(lm=dspy.LM("openai/gpt-5-mini", **gateway_kwargs))
dspy.configure(lm=dspy.LM("openai/gpt-5.5", **gateway_kwargs))
```

<Note>
DSPy's LiteLLM adapter requires an OpenAI transport prefix for this OpenAI-compatible gateway path. Do not represent Claude or Gemini as `openai/...` models in this doc; use the Respan API or OpenAI SDK gateway pages for provider-neutral Claude and Gemini examples.
</Note>

See the [full model list](https://platform.respan.ai/platform/models).
