Skip to content

Commit 8a57690

Browse files
committed
fix tracing project
1 parent 51d7817 commit 8a57690

5 files changed

Lines changed: 414 additions & 292 deletions

File tree

langsmith_tracing/basic/starter.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
from langsmith_tracing.basic.workflows import BasicLLMWorkflow
1313

14+
PROJECT_NAME = "langsmith-basic"
15+
1416

1517
async def main():
1618
add_temporal_runs = "--add-temporal-runs" in sys.argv
@@ -19,7 +21,7 @@ async def main():
1921
config.setdefault("target_host", "localhost:7233")
2022

2123
plugin = LangSmithPlugin(
22-
project_name="langsmith-basic",
24+
project_name=PROJECT_NAME,
2325
add_temporal_runs=add_temporal_runs,
2426
)
2527

@@ -29,9 +31,15 @@ async def main():
2931
plugins=[plugin],
3032
)
3133

32-
# Client-side @traceable wraps the entire workflow call, creating a
33-
# root span in LangSmith that the workflow and activity traces nest under.
34-
@traceable(name="Basic LLM Request", run_type="chain", tags=["client-side"])
34+
@traceable(
35+
name="Basic LLM Request",
36+
run_type="chain",
37+
# CRITICAL: Client-side @traceable runs outside the LangSmithPlugin's scope.
38+
# Make sure client-side traces use the same project_name as what is given to
39+
# # the plugin.
40+
project_name=PROJECT_NAME,
41+
tags=["client-side"],
42+
)
3543
async def run_workflow(prompt: str) -> str:
3644
return await client.execute_workflow(
3745
BasicLLMWorkflow.run,

langsmith_tracing/basic/worker.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ async def main():
2626
config = ClientConfig.load_client_connect_config()
2727
config.setdefault("target_host", "localhost:7233")
2828

29-
client = await Client.connect(
30-
**config,
31-
data_converter=pydantic_data_converter,
32-
)
33-
3429
plugin = LangSmithPlugin(
3530
project_name="langsmith-basic",
3631
add_temporal_runs=add_temporal_runs,
3732
)
3833

34+
client = await Client.connect(
35+
**config,
36+
data_converter=pydantic_data_converter,
37+
plugins=[plugin],
38+
)
39+
3940
async with Worker(
4041
client,
4142
task_queue="langsmith-basic-task-queue",
4243
workflows=[BasicLLMWorkflow],
4344
activities=[call_openai],
44-
plugins=[plugin],
4545
):
4646
label = "with" if add_temporal_runs else "without"
4747
print(f"Worker started ({label} Temporal runs in traces), ctrl+c to exit")

langsmith_tracing/chatbot/starter.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import sys
66
import uuid
77

8-
import langsmith as ls
98
from langsmith import traceable
109
from temporalio.client import Client
1110
from temporalio.contrib.langsmith import LangSmithPlugin
@@ -14,6 +13,8 @@
1413

1514
from langsmith_tracing.chatbot.workflows import ChatbotWorkflow
1615

16+
PROJECT_NAME = "langsmith-chatbot"
17+
1718

1819
async def main():
1920
add_temporal_runs = "--add-temporal-runs" in sys.argv
@@ -22,7 +23,7 @@ async def main():
2223
config.setdefault("target_host", "localhost:7233")
2324

2425
plugin = LangSmithPlugin(
25-
project_name="langsmith-chatbot",
26+
project_name=PROJECT_NAME,
2627
add_temporal_runs=add_temporal_runs,
2728
)
2829

@@ -34,11 +35,13 @@ async def main():
3435

3536
wf_id = f"langsmith-chatbot-{uuid.uuid4().hex[:8]}"
3637

37-
# Client-side trace wraps the full interactive session. Each turn
38-
# (signal + query poll) nests under this root span in LangSmith.
3938
@traceable(
4039
name=f"Chatbot Session {wf_id[-8:]}",
4140
run_type="chain",
41+
# CRITICAL: Client-side @traceable runs outside the LangSmithPlugin's scope.
42+
# Make sure client-side traces use the same project_name as what is given to
43+
# # the plugin.
44+
project_name=PROJECT_NAME,
4245
tags=["client-side", "chatbot"],
4346
)
4447
async def run_session():
@@ -83,10 +86,7 @@ async def send_and_wait(msg: str):
8386
else:
8487
print("(timed out waiting for response)")
8588

86-
try:
87-
await run_session()
88-
finally:
89-
ls.flush()
89+
await run_session()
9090

9191

9292
if __name__ == "__main__":

langsmith_tracing/chatbot/worker.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
from langsmith_tracing.chatbot.activities import call_openai
1414
from langsmith_tracing.chatbot.workflows import ChatbotWorkflow
1515

16-
interrupt_event = asyncio.Event()
17-
1816

1917
async def main():
2018
logging.basicConfig(level=logging.INFO)
@@ -27,30 +25,25 @@ async def main():
2725
client = await Client.connect(
2826
**config,
2927
data_converter=pydantic_data_converter,
28+
plugins=[
29+
LangSmithPlugin(
30+
project_name="langsmith-chatbot",
31+
add_temporal_runs=add_temporal_runs,
32+
)
33+
],
3034
)
3135

32-
plugin = LangSmithPlugin(
33-
project_name="langsmith-chatbot",
34-
add_temporal_runs=add_temporal_runs,
35-
)
36-
37-
async with Worker(
36+
worker = Worker(
3837
client,
3938
task_queue="langsmith-chatbot-task-queue",
4039
workflows=[ChatbotWorkflow],
4140
activities=[call_openai],
42-
plugins=[plugin],
43-
):
44-
label = "with" if add_temporal_runs else "without"
45-
print(f"Worker started ({label} Temporal runs in traces), ctrl+c to exit")
46-
await interrupt_event.wait()
47-
print("Shutting down")
41+
)
42+
43+
label = "with" if add_temporal_runs else "without"
44+
print(f"Worker started ({label} Temporal runs in traces), ctrl+c to exit")
45+
await worker.run()
4846

4947

5048
if __name__ == "__main__":
51-
loop = asyncio.new_event_loop()
52-
try:
53-
loop.run_until_complete(main())
54-
except KeyboardInterrupt:
55-
interrupt_event.set()
56-
loop.run_until_complete(loop.shutdown_asyncgens())
49+
asyncio.run(main())

0 commit comments

Comments
 (0)