@@ -224,18 +224,16 @@ Note that if you do this at the **root** of a trace (i.e., the top-level run, th
224
224
225
225
## Access run (span) ID for LangChain invocations
226
226
227
- When you invoke a LangChain object, you can access the run ID of the invocation. This run ID can be used to query the run in LangSmith.
228
-
229
- In Python, you can use the ` collect_runs ` context manager to access the run ID.
227
+ When you invoke a LangChain object, you can manually specify the run ID of the invocation. This run ID can be used to query the run in LangSmith.
230
228
231
229
In JS/TS, you can use a ` RunCollectorCallbackHandler ` instance to access the run ID.
232
230
233
231
<CodeTabs
234
232
tabs = { [
235
- PythonBlock (` from langchain_openai import ChatOpenAI
233
+ PythonBlock (` import uuid\n
234
+ from langchain_openai import ChatOpenAI
236
235
from langchain_core.prompts import ChatPromptTemplate
237
- from langchain_core.output_parsers import StrOutputParser
238
- from langchain_core.tracers.context import collect_runs\n
236
+ from langchain_core.output_parsers import StrOutputParser\n
239
237
prompt = ChatPromptTemplate.from_messages([
240
238
("system", "You are a helpful assistant. Please respond to the user's request only based on the given context."),
241
239
("user", "Question: {question}\\ n\\ nContext: {context}")
@@ -245,12 +243,9 @@ output_parser = StrOutputParser()\n
245
243
chain = prompt | model | output_parser\n
246
244
question = "Can you summarize this morning's meetings?"
247
245
context = "During this morning's meeting, we solved all world conflict."
248
- with collect_runs() as cb:
249
- result = chain.invoke({"question": question, "context": context})
250
- # Get the root run id
251
- # highlight-next-line
252
- run_id = cb.traced_runs[0].id
253
- print(run_id) ` ),
246
+ my_uuid = uuid.uuid4()
247
+ result = chain.invoke({"question": question, "context": context}, {"run_id": my_uuid})
248
+ print(my_uuid) ` ),
254
249
TypeScriptBlock (` import { ChatOpenAI } from "@langchain/openai";
255
250
import { ChatPromptTemplate } from "@langchain/core/prompts";
256
251
import { StringOutputParser } from "@langchain/core/output_parsers";
0 commit comments