core: tracer: remove numeric execution order (#21220)

- this hasn't been used in a long time and requires some additional
bookkeeping i'm going to streamline in the next pr
pull/21230/head
Nuno Campos 4 weeks ago committed by GitHub
parent 6ac6158a07
commit 47ce8d5a57
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -2010,6 +2010,10 @@ def _configure(
if run_tree is not None:
for handler in callback_manager.handlers:
if isinstance(handler, LangChainTracer):
handler.order_map[run_tree.id] = (
run_tree.trace_id,
run_tree.dotted_order,
)
handler.run_map[str(run_tree.id)] = cast(Run, run_tree)
for var, inheritable, handler_class, env_var in _configure_hooks:
create_one = (

@ -16,6 +16,7 @@ from typing import (
Optional,
Sequence,
Set,
Tuple,
Union,
cast,
)
@ -68,6 +69,9 @@ class BaseTracer(BaseCallbackHandler, ABC):
super().__init__(**kwargs)
self._schema_format = _schema_format # For internal use only API will change.
self.run_map: Dict[str, Run] = {}
"""Map of run ID to run. Cleared on run end."""
self.order_map: Dict[UUID, Tuple[UUID, str]] = {}
"""Map of run ID to (trace_id, dotted_order). Cleared when tracer GCed."""
@staticmethod
def _add_child_run(
@ -100,30 +104,23 @@ class BaseTracer(BaseCallbackHandler, ABC):
"""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:
self._add_child_run(parent_run, run)
if hasattr(parent_run, "child_execution_order"):
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."
)
if parent := self.order_map.get(run.parent_run_id):
run.trace_id, run.dotted_order = parent
run.dotted_order += "." + current_dotted_order
if parent_run := self.run_map.get(str(run.parent_run_id)):
self._add_child_run(parent_run, run)
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.")
logger.warning(
f"Parent run {run.parent_run_id} not found for run {run.id}."
" Treating as a root run."
)
run.parent_run_id = None
run.trace_id = run.id
run.dotted_order = current_dotted_order
else:
run.trace_id = run.id
run.dotted_order = current_dotted_order
self.order_map[run.id] = (run.trace_id, run.dotted_order)
self.run_map[str(run.id)] = run
self._on_run_create(run)
@ -131,36 +128,9 @@ class BaseTracer(BaseCallbackHandler, ABC):
"""End a trace for a run."""
if not run.parent_run_id:
self._persist_run(run)
else:
parent_run = self.run_map.get(str(run.parent_run_id))
if parent_run is None:
logger.debug(f"Parent run with UUID {run.parent_run_id} not found.")
elif (
run.child_execution_order is not None
and getattr(parent_run, "child_execution_order", None) is not None
and run.child_execution_order > parent_run.child_execution_order
):
parent_run.child_execution_order = run.child_execution_order
self.run_map.pop(str(run.id))
self._on_run_update(run)
def _get_execution_order(self, parent_run_id: Optional[str] = None) -> int:
"""Get the execution order for a run."""
if parent_run_id is None:
return 1
parent_run = self.run_map.get(parent_run_id)
if parent_run is None:
logger.debug(f"Parent run with UUID {parent_run_id} not found.")
return 1
if getattr(parent_run, "child_execution_order", None) is None:
logger.debug(
f"Parent run with UUID {parent_run_id} has no child_execution_order."
)
return 1
return parent_run.child_execution_order + 1
def _get_run(
self, run_id: UUID, run_type: Union[str, Set[str], None] = None
) -> Run:
@ -205,8 +175,6 @@ class BaseTracer(BaseCallbackHandler, ABC):
f"Chat model tracing is not supported in "
f"for {self._schema_format} format."
)
parent_run_id_ = str(parent_run_id) if parent_run_id else None
execution_order = self._get_execution_order(parent_run_id_)
start_time = datetime.now(timezone.utc)
if metadata:
kwargs.update({"metadata": metadata})
@ -218,8 +186,6 @@ class BaseTracer(BaseCallbackHandler, ABC):
extra=kwargs,
events=[{"name": "start", "time": start_time}],
start_time=start_time,
execution_order=execution_order,
child_execution_order=execution_order,
# WARNING: This is valid ONLY for streaming_events.
# run_type="llm" is what's used by virtually all tracers.
# Changing this to "chat_model" may break triggering on_llm_start
@ -244,8 +210,6 @@ class BaseTracer(BaseCallbackHandler, ABC):
**kwargs: Any,
) -> Run:
"""Start a trace for an LLM run."""
parent_run_id_ = str(parent_run_id) if parent_run_id else None
execution_order = self._get_execution_order(parent_run_id_)
start_time = datetime.now(timezone.utc)
if metadata:
kwargs.update({"metadata": metadata})
@ -258,8 +222,6 @@ class BaseTracer(BaseCallbackHandler, ABC):
extra=kwargs,
events=[{"name": "start", "time": start_time}],
start_time=start_time,
execution_order=execution_order,
child_execution_order=execution_order,
run_type="llm",
tags=tags or [],
name=name, # type: ignore[arg-type]
@ -376,8 +338,6 @@ class BaseTracer(BaseCallbackHandler, ABC):
**kwargs: Any,
) -> Run:
"""Start a trace for a chain run."""
parent_run_id_ = str(parent_run_id) if parent_run_id else None
execution_order = self._get_execution_order(parent_run_id_)
start_time = datetime.now(timezone.utc)
if metadata:
kwargs.update({"metadata": metadata})
@ -389,8 +349,6 @@ class BaseTracer(BaseCallbackHandler, ABC):
extra=kwargs,
events=[{"name": "start", "time": start_time}],
start_time=start_time,
execution_order=execution_order,
child_execution_order=execution_order,
child_runs=[],
run_type=run_type or "chain",
name=name, # type: ignore[arg-type]
@ -474,8 +432,6 @@ class BaseTracer(BaseCallbackHandler, ABC):
**kwargs: Any,
) -> Run:
"""Start a trace for a tool run."""
parent_run_id_ = str(parent_run_id) if parent_run_id else None
execution_order = self._get_execution_order(parent_run_id_)
start_time = datetime.now(timezone.utc)
if metadata:
kwargs.update({"metadata": metadata})
@ -496,8 +452,6 @@ class BaseTracer(BaseCallbackHandler, ABC):
extra=kwargs,
events=[{"name": "start", "time": start_time}],
start_time=start_time,
execution_order=execution_order,
child_execution_order=execution_order,
child_runs=[],
run_type="tool",
tags=tags or [],
@ -546,8 +500,6 @@ class BaseTracer(BaseCallbackHandler, ABC):
**kwargs: Any,
) -> Run:
"""Run when Retriever starts running."""
parent_run_id_ = str(parent_run_id) if parent_run_id else None
execution_order = self._get_execution_order(parent_run_id_)
start_time = datetime.now(timezone.utc)
if metadata:
kwargs.update({"metadata": metadata})
@ -560,8 +512,6 @@ class BaseTracer(BaseCallbackHandler, ABC):
extra=kwargs,
events=[{"name": "start", "time": start_time}],
start_time=start_time,
execution_order=execution_order,
child_execution_order=execution_order,
tags=tags,
child_runs=[],
run_type="retriever",

@ -105,8 +105,6 @@ class LangChainTracer(BaseTracer):
**kwargs: Any,
) -> Run:
"""Start a trace for an LLM run."""
parent_run_id_ = str(parent_run_id) if parent_run_id else None
execution_order = self._get_execution_order(parent_run_id_)
start_time = datetime.now(timezone.utc)
if metadata:
kwargs.update({"metadata": metadata})
@ -118,8 +116,6 @@ class LangChainTracer(BaseTracer):
extra=kwargs,
events=[{"name": "start", "time": start_time}],
start_time=start_time,
execution_order=execution_order,
child_execution_order=execution_order,
run_type="llm",
tags=tags,
name=name, # type: ignore[arg-type]

@ -113,8 +113,6 @@ class ToolRun(BaseRun):
class Run(BaseRunV2):
"""Run schema for the V2 API in the Tracer."""
execution_order: int
child_execution_order: int
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)

@ -68,9 +68,9 @@ class FunctionCallbackHandler(BaseTracer):
def get_breadcrumbs(self, run: Run) -> str:
parents = self.get_parents(run)[::-1]
string = " > ".join(
f"{parent.execution_order}:{parent.run_type}:{parent.name}"
f"{parent.run_type}:{parent.name}"
if i != len(parents) - 1
else f"{parent.execution_order}:{parent.run_type}:{parent.name}"
else f"{parent.run_type}:{parent.name}"
for i, parent in enumerate(parents + [run])
)
return string

File diff suppressed because one or more lines are too long

@ -134,8 +134,6 @@ class FakeTracer(BaseTracer):
self.uuids_map[run.parent_run_id] if run.parent_run_id else None
),
"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,
"inputs": (

@ -68,8 +68,6 @@ def test_tracer_llm_run() -> None:
{"name": "end", "time": datetime.now(timezone.utc)},
],
extra={},
execution_order=1,
child_execution_order=1,
serialized=SERIALIZED,
inputs={"prompts": []},
outputs=LLMResult(generations=[[]]), # type: ignore[arg-type]
@ -103,8 +101,6 @@ def test_tracer_chat_model_run() -> None:
{"name": "end", "time": datetime.now(timezone.utc)},
],
extra={},
execution_order=1,
child_execution_order=1,
serialized=SERIALIZED_CHAT,
inputs=dict(prompts=["Human: "]),
outputs=LLMResult(generations=[[]]), # type: ignore[arg-type]
@ -141,8 +137,6 @@ def test_tracer_multiple_llm_runs() -> None:
{"name": "end", "time": datetime.now(timezone.utc)},
],
extra={},
execution_order=1,
child_execution_order=1,
serialized=SERIALIZED,
inputs=dict(prompts=[]),
outputs=LLMResult(generations=[[]]), # type: ignore[arg-type]
@ -174,8 +168,6 @@ def test_tracer_chain_run() -> None:
{"name": "end", "time": datetime.now(timezone.utc)},
],
extra={},
execution_order=1,
child_execution_order=1,
serialized={"name": "chain"},
inputs={},
outputs={},
@ -204,8 +196,6 @@ def test_tracer_tool_run() -> None:
{"name": "end", "time": datetime.now(timezone.utc)},
],
extra={},
execution_order=1,
child_execution_order=1,
serialized={"name": "tool"},
inputs={"input": "test"},
outputs={"output": "test"},
@ -266,8 +256,6 @@ def test_tracer_nested_run() -> None:
{"name": "end", "time": datetime.now(timezone.utc)},
],
extra={},
execution_order=1,
child_execution_order=4,
serialized={"name": "chain"},
inputs={},
outputs={},
@ -285,8 +273,6 @@ def test_tracer_nested_run() -> None:
{"name": "end", "time": datetime.now(timezone.utc)},
],
extra={},
execution_order=2,
child_execution_order=3,
serialized={"name": "tool"},
inputs=dict(input="test"),
outputs=dict(output="test"),
@ -306,8 +292,6 @@ def test_tracer_nested_run() -> None:
{"name": "end", "time": datetime.now(timezone.utc)},
],
extra={},
execution_order=3,
child_execution_order=3,
serialized=SERIALIZED,
inputs=dict(prompts=[]),
outputs=LLMResult(generations=[[]]), # type: ignore[arg-type]
@ -328,8 +312,6 @@ def test_tracer_nested_run() -> None:
{"name": "end", "time": datetime.now(timezone.utc)},
],
extra={},
execution_order=4,
child_execution_order=4,
serialized=SERIALIZED,
inputs=dict(prompts=[]),
outputs=LLMResult(generations=[[]]), # type: ignore[arg-type]
@ -358,8 +340,6 @@ def test_tracer_llm_run_on_error() -> None:
{"name": "error", "time": datetime.now(timezone.utc)},
],
extra={},
execution_order=1,
child_execution_order=1,
serialized=SERIALIZED,
inputs=dict(prompts=[]),
outputs=None,
@ -391,8 +371,6 @@ def test_tracer_llm_run_on_error_callback() -> None:
{"name": "error", "time": datetime.now(timezone.utc)},
],
extra={},
execution_order=1,
child_execution_order=1,
serialized=SERIALIZED,
inputs=dict(prompts=[]),
outputs=None,
@ -429,8 +407,6 @@ def test_tracer_chain_run_on_error() -> None:
{"name": "error", "time": datetime.now(timezone.utc)},
],
extra={},
execution_order=1,
child_execution_order=1,
serialized={"name": "chain"},
inputs={},
outputs=None,
@ -461,8 +437,6 @@ def test_tracer_tool_run_on_error() -> None:
{"name": "error", "time": datetime.now(timezone.utc)},
],
extra={},
execution_order=1,
child_execution_order=1,
serialized={"name": "tool"},
inputs=dict(input="test"),
outputs=None,
@ -534,8 +508,6 @@ def test_tracer_nested_runs_on_error() -> None:
{"name": "error", "time": datetime.now(timezone.utc)},
],
extra={},
execution_order=1,
child_execution_order=5,
serialized={"name": "chain"},
error=repr(exception),
inputs={},
@ -554,8 +526,6 @@ def test_tracer_nested_runs_on_error() -> None:
{"name": "end", "time": datetime.now(timezone.utc)},
],
extra={},
execution_order=2,
child_execution_order=2,
serialized=SERIALIZED,
error=None,
inputs=dict(prompts=[]),
@ -574,8 +544,6 @@ def test_tracer_nested_runs_on_error() -> None:
{"name": "end", "time": datetime.now(timezone.utc)},
],
extra={},
execution_order=3,
child_execution_order=3,
serialized=SERIALIZED,
error=None,
inputs=dict(prompts=[]),
@ -594,8 +562,6 @@ def test_tracer_nested_runs_on_error() -> None:
{"name": "error", "time": datetime.now(timezone.utc)},
],
extra={},
execution_order=4,
child_execution_order=5,
serialized={"name": "tool"},
error=repr(exception),
inputs=dict(input="test"),
@ -614,8 +580,6 @@ def test_tracer_nested_runs_on_error() -> None:
{"name": "error", "time": datetime.now(timezone.utc)},
],
extra={},
execution_order=5,
child_execution_order=5,
serialized=SERIALIZED,
error=repr(exception),
inputs=dict(prompts=[]),

@ -67,6 +67,7 @@ def test_tracer_with_run_tree_parent() -> None:
parent = RunTree(name="parent", inputs={"input": "foo"}, client=client)
run_id = uuid.uuid4()
tracer = LangChainTracer(client=client)
tracer.order_map[parent.id] = (parent.trace_id, parent.dotted_order)
tracer.run_map[str(parent.id)] = parent # type: ignore
tracer.on_chain_start(
{"name": "child"}, {"input": "bar"}, run_id=run_id, parent_run_id=parent.id

Loading…
Cancel
Save