Skip to main content
What you’ll build: A DSPy program running on a SambaCloud-hosted model, with verified examples for a basic prediction, tool-calling agent, structured output, and a prompt optimizer run.

Overview

DSPy is a framework from Stanford NLP for programming, rather than prompting, language models. It compiles declarative signatures into prompts and can automatically optimize those prompts against a metric and a training set. Wire it to SambaCloud and you get:

No framework changes

dspy.LM accepts an openai/-prefixed model name plus api_base and api_key, so pointing it at SambaCloud is a configuration change, not a code change.

Tool-calling agents

dspy.ReAct wraps plain Python functions as tools and runs a real reasoning-and-acting loop against your model.

Structured output

Declare a Pydantic model as a signature’s output field and get a validated object back.

Prompt optimization

Optimizers like BootstrapFewShot run real inference against SambaCloud to compile better prompts from a handful of labeled examples, which fits an inference-heavy usage profile well.

Prerequisites

Setup

1

Create and activate a virtual environment

2

Install DSPy

3

Export your SambaCloud API key

Never commit your API key to source control. Use a .env file or your shell’s secret manager.

Example: basic prediction

dspy.LM routes through LiteLLM under the hood. Prefix the model name with openai/ and pass api_base/api_key to route to SambaCloud:
basic_example.py
Run it:
Output
Swap "MiniMax-M3" for any model on the SambaCloud supported models page, keeping the openai/ prefix.

Example: tool calling with ReAct

dspy.ReAct turns plain Python functions into tools and runs a reasoning-and-acting loop: the model decides which tool to call, DSPy runs it, and feeds the result back until the model finishes.
react_example.py
Run it:
Output
ReAct records every thought, tool call, and observation in result.trajectory, which confirms the model actually called get_weather rather than guessing a plausible answer:

Example: structured output

Declare a Pydantic model as a signature’s output field type. DSPy does not use the API’s response_format parameter for this: it writes the JSON schema directly into the prompt and parses the model’s response itself, so this works even on models without native structured-output support.
structured_example.py
Run it:
Output
Inspecting lm.history[-1]["messages"] shows the real request: the JSON schema is embedded as text in the system prompt, not sent as a response_format parameter.
System message (excerpt)
DSPy then parses the model’s formatted text response and validates it against the Pydantic model, which is what turns prose output into the CityInfo(...) object result.info returns.

Example: optimize a program

Optimizers compile a program by running real inference against your training set and keeping the examples that improve a metric. BootstrapFewShot is the simplest one:
optimize_example.py
Run it:
Output
Each optimizer run makes one real inference call against SambaCloud per bootstrap attempt, on top of the calls your program makes when evaluated. This usage profile, inference-heavy by design, is the main reason DSPy’s cost scales with trainset size and optimizer rounds, not just with how often the compiled program is later called.

Additional resources

DSPy repository

Source code, examples, and release notes for DSPy.

DSPy documentation

Official guides covering signatures, modules, optimizers, and evaluation.

Supported models

Current list of models available on SambaCloud.

Function calling and JSON mode

Which SambaCloud models support tool calling and structured output natively.

Troubleshooting

Install with pip install dspy, then confirm your virtual environment is active: which python3 should point inside .venv.
Export the key before running: export SAMBANOVA_API_KEY="your-key". Verify with echo $SAMBANOVA_API_KEY.
The model name is missing the openai/ prefix. DSPy routes through LiteLLM, which uses the prefix to pick the request format. Use "openai/MiniMax-M3", not "MiniMax-M3".
The key in SAMBANOVA_API_KEY is wrong or expired. Generate a new one in the SambaCloud portal and export it again.
dspy.ReAct doesn’t use the API’s native function calling. It describes your tools in the prompt and parses the model’s reply to decide which tool to run. If the model guesses an answer instead, check result.trajectory for a tool_name_0 entry, then run dspy.inspect_history(n=1) to see the exact prompt and reply. Give the tool a clear docstring and type hints, since that text is all the model sees.
The model’s reply didn’t follow the field format DSPy expected. Run dspy.inspect_history(n=1) to see the raw response. Retrying usually succeeds; if it fails consistently, try another model from the supported models page.