NeMo Relay LangChain Integration

View as Markdown

Use the nemo_relay.integrations.langchain package to add NeMo Relay observability to LangChain agents.

Setup

Install the LangChain integration extra in your application environment.

$uv add "nemo-relay[langchain]"

The example below uses the NVIDIA LangChain provider. Install that provider extra too if you want to run the example as written:

$uv add "nemo-relay[langchain,langchain-nvidia]"

Usage Example

1import asyncio
2
3import nemo_relay
4from langchain.agents import create_agent
5from langchain_core.tools import tool
6from nemo_relay.integrations.langchain import NemoRelayCallbackHandler, NemoRelayMiddleware
7
8@tool
9def get_weather(location: str) -> str:
10 """Get the current weather for a location."""
11 return f"The weather in {location} is sunny and 72 degrees."
12
13agent = create_agent(
14 model="nvidia:nvidia/nemotron-3-nano-30b-a3b",
15 tools=[get_weather],
16 middleware=[NemoRelayMiddleware()],
17 system_prompt="Use tools when they are relevant. Keep the final answer brief.",
18)
19
20input_payload = {
21 "messages": [
22 {
23 "role": "user",
24 "content": "What is the weather in San Francisco?",
25 }
26 ]
27}
28
29with nemo_relay.scope.scope("langchain-request", nemo_relay.ScopeType.Agent):
30 result = asyncio.run(
31 agent.ainvoke(input_payload, config={"callbacks": [NemoRelayCallbackHandler()]})
32 )
33
34final_message = result["messages"][-1]
35print(f"Final response: {final_message.content}")

When the agent uses ChatNVIDIA, NeMo Relay forwards request headers, including Dynamo session lineage, as HTTP headers. They are not added to the NIM request body.

Verify the Integration

The integration works correctly when:

  • The agent run completes and prints a final response.
  • The langchain-request scope contains the managed model call.
  • Tool activity appears under the same request when the agent decides to use get_weather.

Observability

Refer to Observability for details on exporting NeMo Relay observability data to third-party systems.