LangSmith/Q/Is it possible to log a string to a trace in langsmith
Properties
- tags
- Question/My
Is it possible to log a string to a LangSmith/Trace?
- Impetus: it's difficult to get access to log messages in langgraph/Cloud. Could we instead log onto the trace?
Research
- Yes, there are several ways to add custom data to a LangSmith trace:
- The metadata can be any JSON-serializable key-value pairs
- You can view these logs in the LangSmith UI or query them programmatically (docs)
- This provides a way to add logging information that will be associated with the specific trace/run
My Questions - Follow-Up
What about if I'm running within langgraph/Cloud or langgraph/Studio? All interactions are automatically logged to langsmith without needing to add ls.trace. should I just use the context manager?
- When running in LangGraph Cloud/Studio:
- All LangGraph operations are automatically traced to LangSmith
- You have two main options for adding custom logs:
- 1. Use
get_current_run_tree()to access the current trace:- python
from langsmith import get_current_run_tree def my_node(): rt = get_current_run_tree() rt.metadata["my_log"] = "some message" # ... rest of node logicUse the context manager for specific sub-sections:
- python
from langsmith import trace def my_node(): with trace( name="sub-operation", metadata={"log": "message"} ) as rt: # ... sub-operation logic rt.metadata["another_log"] = "another message"
- 1. Use
- The first approach is simpler if you just want to add logs to the existing trace
- The second approach is better if you want to create a separate sub-trace
- When running in LangGraph Cloud/Studio:
What about if I'm inside a test function marked with LangSmith/PyTest/mark
@pytest.mark.langsmith?- When using
@pytest.mark.langsmith:- The test function is automatically traced
- You can use the
testingmodule to log data (docs):- python
from langsmith import testing as t langsmith def test_my_function(): # Log test inputs t.log_inputs({"input_key": "input_value"}) # Run your test logic result = my_function() # Log test outputs t.log_outputs({"output_key": result}) # Add custom feedback/logs with t.trace_feedback(): t.log_feedback(key="my_log", score=1.0)
- The test results and all logged data will be available in the LangSmith UI
- You can group tests into test suites using
LANGSMITH_TEST_SUITEenvironment variable - Test runs can be cached using
LANGSMITH_TEST_CACHEto save resources
- When using