Calculate trace_id and dotted_order client side (#15351)

<!-- Thank you for contributing to LangChain!

Please title your PR "<package>: <description>", where <package> is
whichever of langchain, community, core, experimental, etc. is being
modified.

Replace this entire comment with:
  - **Description:** a description of the change, 
  - **Issue:** the issue # it fixes if applicable,
  - **Dependencies:** any dependencies required for this change,
- **Twitter handle:** we announce bigger features on Twitter. If your PR
gets announced, and you'd like a mention, we'll gladly shout you out!

Please make sure your PR is passing linting and testing before
submitting. Run `make format`, `make lint` and `make test` from the root
of the package you've modified to check this locally.

See contribution guidelines for more information on how to write/run
tests, lint, etc: https://python.langchain.com/docs/contributing/

If you're adding a new integration, please include:
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in
`docs/docs/integrations` directory.

If no one reviews your PR within a few days, please @-mention one of
@baskaryan, @eyurtsev, @hwchase17.
 -->
pull/15421/head^2
Ankush Gola 9 months ago committed by GitHub
parent a8f6f33cd9
commit f50dba12ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -73,6 +73,7 @@ class BaseTracer(BaseCallbackHandler, ABC):
def _start_trace(self, run: Run) -> None:
"""Start a trace for a run."""
current_dotted_order = run.start_time.strftime("%Y%m%dT%H%M%S%fZ") + str(run.id)
if run.parent_run_id:
parent_run = self.run_map.get(str(run.parent_run_id))
if parent_run:
@ -80,8 +81,23 @@ class BaseTracer(BaseCallbackHandler, ABC):
parent_run.child_execution_order = max(
parent_run.child_execution_order, run.child_execution_order
)
run.trace_id = parent_run.trace_id
if parent_run.dotted_order:
run.dotted_order = (
parent_run.dotted_order + "." + current_dotted_order
)
else:
# Something wrong with tracer parent run has no dotted_order
logger.debug(
f"Parent run with UUID {run.parent_run_id} has no dotted_order."
)
else:
# Something wrong with tracer, parent run not found
# Calculate the trace_id and dotted_order server side
logger.debug(f"Parent run with UUID {run.parent_run_id} not found.")
else:
run.trace_id = run.id
run.dotted_order = current_dotted_order
self.run_map[str(run.id)] = run
self._on_run_create(run)

@ -2,6 +2,7 @@
from __future__ import annotations
from typing import Any, Dict, List, Optional
from uuid import UUID
from langsmith.schemas import RunBase as BaseRunV2
@ -16,6 +17,8 @@ class Run(BaseRunV2):
child_runs: List[Run] = Field(default_factory=list)
tags: Optional[List[str]] = Field(default_factory=list)
events: List[Dict[str, Any]] = Field(default_factory=list)
trace_id: Optional[UUID] = None
dotted_order: Optional[str] = None
@root_validator(pre=True)
def assign_name(cls, values: dict) -> dict:

@ -70,6 +70,8 @@ def test_tracer_llm_run() -> None:
outputs=LLMResult(generations=[[]]),
error=None,
run_type="llm",
trace_id=uuid,
dotted_order=f"20230101T000000000000Z{uuid}",
)
tracer = FakeTracer()
@ -103,6 +105,8 @@ def test_tracer_chat_model_run() -> None:
outputs=LLMResult(generations=[[]]),
error=None,
run_type="llm",
trace_id=run_managers[0].run_id,
dotted_order=f"20230101T000000000000Z{run_managers[0].run_id}",
)
for run_manager in run_managers:
run_manager.on_llm_end(response=LLMResult(generations=[[]]))
@ -139,6 +143,8 @@ def test_tracer_multiple_llm_runs() -> None:
outputs=LLMResult(generations=[[]]),
error=None,
run_type="llm",
trace_id=uuid,
dotted_order=f"20230101T000000000000Z{uuid}",
)
tracer = FakeTracer()
@ -170,6 +176,8 @@ def test_tracer_chain_run() -> None:
outputs={},
error=None,
run_type="chain",
trace_id=uuid,
dotted_order=f"20230101T000000000000Z{uuid}",
)
tracer = FakeTracer()
@ -198,6 +206,8 @@ def test_tracer_tool_run() -> None:
outputs={"output": "test"},
error=None,
run_type="tool",
trace_id=uuid,
dotted_order=f"20230101T000000000000Z{uuid}",
)
tracer = FakeTracer()
tracer.on_tool_start(serialized={"name": "tool"}, input_str="test", run_id=uuid)
@ -257,6 +267,8 @@ def test_tracer_nested_run() -> None:
inputs={},
outputs={},
run_type="chain",
trace_id=chain_uuid,
dotted_order=f"20230101T000000000000Z{chain_uuid}",
child_runs=[
Run(
id=tool_uuid,
@ -275,6 +287,8 @@ def test_tracer_nested_run() -> None:
outputs=dict(output="test"),
error=None,
run_type="tool",
trace_id=chain_uuid,
dotted_order=f"20230101T000000000000Z{chain_uuid}.20230101T000000000000Z{tool_uuid}",
child_runs=[
Run(
id=str(llm_uuid1),
@ -293,6 +307,8 @@ def test_tracer_nested_run() -> None:
inputs=dict(prompts=[]),
outputs=LLMResult(generations=[[]]),
run_type="llm",
trace_id=chain_uuid,
dotted_order=f"20230101T000000000000Z{chain_uuid}.20230101T000000000000Z{tool_uuid}.20230101T000000000000Z{llm_uuid1}",
)
],
),
@ -313,6 +329,8 @@ def test_tracer_nested_run() -> None:
inputs=dict(prompts=[]),
outputs=LLMResult(generations=[[]]),
run_type="llm",
trace_id=chain_uuid,
dotted_order=f"20230101T000000000000Z{chain_uuid}.20230101T000000000000Z{llm_uuid2}",
),
],
)
@ -342,6 +360,8 @@ def test_tracer_llm_run_on_error() -> None:
outputs=None,
error=repr(exception),
run_type="llm",
trace_id=uuid,
dotted_order=f"20230101T000000000000Z{uuid}",
)
tracer = FakeTracer()
@ -373,6 +393,8 @@ def test_tracer_llm_run_on_error_callback() -> None:
outputs=None,
error=repr(exception),
run_type="llm",
trace_id=uuid,
dotted_order=f"20230101T000000000000Z{uuid}",
)
class FakeTracerWithLlmErrorCallback(FakeTracer):
@ -410,6 +432,8 @@ def test_tracer_chain_run_on_error() -> None:
outputs=None,
error=repr(exception),
run_type="chain",
trace_id=uuid,
dotted_order=f"20230101T000000000000Z{uuid}",
)
tracer = FakeTracer()
@ -441,6 +465,8 @@ def test_tracer_tool_run_on_error() -> None:
action="{'name': 'tool'}",
error=repr(exception),
run_type="tool",
trace_id=uuid,
dotted_order=f"20230101T000000000000Z{uuid}",
)
tracer = FakeTracer()
@ -511,6 +537,8 @@ def test_tracer_nested_runs_on_error() -> None:
inputs={},
outputs=None,
run_type="chain",
trace_id=chain_uuid,
dotted_order=f"20230101T000000000000Z{chain_uuid}",
child_runs=[
Run(
id=str(llm_uuid1),
@ -529,6 +557,8 @@ def test_tracer_nested_runs_on_error() -> None:
inputs=dict(prompts=[]),
outputs=LLMResult(generations=[[]], llm_output=None),
run_type="llm",
trace_id=chain_uuid,
dotted_order=f"20230101T000000000000Z{chain_uuid}.20230101T000000000000Z{llm_uuid1}",
),
Run(
id=str(llm_uuid2),
@ -547,6 +577,8 @@ def test_tracer_nested_runs_on_error() -> None:
inputs=dict(prompts=[]),
outputs=LLMResult(generations=[[]], llm_output=None),
run_type="llm",
trace_id=chain_uuid,
dotted_order=f"20230101T000000000000Z{chain_uuid}.20230101T000000000000Z{llm_uuid2}",
),
Run(
id=str(tool_uuid),
@ -565,6 +597,8 @@ def test_tracer_nested_runs_on_error() -> None:
inputs=dict(input="test"),
outputs=None,
action="{'name': 'tool'}",
trace_id=chain_uuid,
dotted_order=f"20230101T000000000000Z{chain_uuid}.20230101T000000000000Z{tool_uuid}",
child_runs=[
Run(
id=str(llm_uuid3),
@ -583,6 +617,8 @@ def test_tracer_nested_runs_on_error() -> None:
inputs=dict(prompts=[]),
outputs=None,
run_type="llm",
trace_id=chain_uuid,
dotted_order=f"20230101T000000000000Z{chain_uuid}.20230101T000000000000Z{tool_uuid}.20230101T000000000000Z{llm_uuid3}",
)
],
run_type="tool",

File diff suppressed because one or more lines are too long

@ -101,6 +101,17 @@ class FakeTracer(BaseTracer):
return self.uuids_map[uuid]
def _copy_run(self, run: Run) -> Run:
if run.dotted_order:
levels = run.dotted_order.split(".")
processed_levels = []
for level in levels:
timestamp, run_id = level.split("Z")
new_run_id = self._replace_uuid(UUID(run_id))
processed_level = f"{timestamp}Z{new_run_id}"
processed_levels.append(processed_level)
new_dotted_order = ".".join(processed_levels)
else:
new_dotted_order = None
return run.copy(
update={
"id": self._replace_uuid(run.id),
@ -110,6 +121,8 @@ class FakeTracer(BaseTracer):
"child_runs": [self._copy_run(child) for child in run.child_runs],
"execution_order": None,
"child_execution_order": None,
"trace_id": self._replace_uuid(run.trace_id) if run.trace_id else None,
"dotted_order": new_dotted_order,
}
)

Loading…
Cancel
Save