Skip to main content
This article walks you through using LangChain to interact with SambaNova models: from setup to instantiation, invocation, streaming, and chaining. LangChain is a software framework for developing applications powered by LLMs (large language models).

Prerequisites

Before you begin, ensure you have:
  • A SambaCloud account and API key. See API keys and URLs.
  • Python 3.10 or later. Version 1.x of langchain-sambanova requires Python 3.10 or later. On Python 3.9, pip installs the much older 0.2.0 release instead of reporting an error.

Setup

Do the following to access ChatSambaNova models:
  1. Create a SambaCloud account and get an API key.
  2. Run the command below to install the langchain-sambanova integration package.
Keep the version bounds. Release 1.0.0 removed the ChatSambaNovaCloud and ChatSambaStudio classes, so this package does ship breaking major releases. An unbounded pip install langchain-sambanova can pick up a future major that breaks the examples on this page.
Confirm which version you installed:
You should see an output similar to the one below.

Credentials

Register the API key you received from cloud.sambanova.ai as an environment variable. ChatSambaNova reads SAMBANOVA_API_KEY on its own, so you never have to pass the key in code.
To set the key from inside a Python session or a notebook, prompt for it at runtime instead of hardcoding it in your source:
SambaCloud users only need the API key. Requests default to https://api.sambanova.ai/v1. SambaStack users also set SAMBANOVA_API_BASE to their deployment URL, or pass base_url to ChatSambaNova.

Instantiation

Now you can instantiate a ChatSambaNova model object and generate chat completions, as shown in the example below. The following creates a ChatSambaNova model object configured for Meta’s Llama 3.3 70B:
Pass any model ID that SambaCloud currently serves. The catalog changes over time, so confirm the ID against SambaCloud models or list it directly with curl https://api.sambanova.ai/v1/models.

Invocation

Pass a system prompt and a user message to the model:
You should see an output similar to the one below.

Streaming

To display tokens as the model generates them, call stream() instead of invoke() and print the content of each chunk. Each chunk is an AIMessageChunk, and concatenating their content values reproduces the full response.
You should see an output similar to the one below, printed incrementally rather than all at once.

Chaining

Chain the model with a prompt template to handle dynamic language translation:
You should see an output similar to the one below.

Tool calling

Bind Python functions to the model with bind_tools. LangChain converts each function’s signature and docstring into a JSON Schema and sends it in the request’s tools field, so the docstring is what tells the model when to call it.
You should see an output similar to the one below.
The model returns the call it wants made, not the result. Your code runs the function and passes the output back:
response.tool_calls is empty when the model answers directly instead of calling a tool, so branch on it rather than assuming a call happened.
Tool calling requires a model that supports it. See Function calling for the current list. Meta-Llama-3.3-70B-Instruct is supported; gemma-4-31B-it is not, and on an unsupported model the request returns a normal text answer with no error to indicate the tools were ignored.

Structured output

Use with_structured_output to get a validated Pydantic object instead of text. This does not use tool calling. It sends your schema as response_format with type: json_schema and parses the reply into your model, so it depends on structured output support rather than on function calling.
You should see an output similar to the one below.
The result is a real Ticket instance, so ticket.priority is typed and any value outside the Literal is rejected before your code sees it. Field descriptions are sent as part of the schema, so they are worth writing carefully.

Troubleshooting

The variable is not set in the environment that runs your script. Export it as shown in Credentials, or pass api_key="..." to ChatSambaNova.
The key is wrong, revoked, or carries a stray quote or newline. Generate a new one at cloud.sambanova.ai/apis.
ChatSambaNovaCloud was removed in langchain-sambanova 1.0.0 and is now an empty deprecated stub that accepts no arguments. Use ChatSambaNova instead, as shown in Instantiation.
You are on 0.1.x, which only shipped ChatSambaNovaCloud. Upgrade with pip install -U "langchain-sambanova>=1.1,<2".
You are running Python 3.9 or earlier. Version 1.x requires Python 3.10 or later. Upgrade Python, then reinstall.
Version 1.x reads the base URL from SAMBANOVA_API_BASE. SAMBANOVA_URL was the 0.1.x variable and no longer has any effect.
You exceeded the requests allowed for your tier. Back off and retry, or reduce concurrency. See Rate limits for the limits that apply to you.
The reply was not valid JSON for your schema, which happens on a model without structured output support. Confirm the model on Function calling, and simplify deeply nested schemas.
The model either chose not to call the tool or does not support tool calling. Check it against Function calling, and make the tool’s docstring state plainly when it should be used.

Additional resources

View the following resources for more information: