Skip to content

Commit ddc850c

Browse files
authored
core: In LangChainTracer, send only the first token event (#31591)
- only the first one is used for analytics
1 parent 50f9354 commit ddc850c

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

libs/core/langchain_core/tracers/langchain.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def __init__(
9595
self.client = client or get_client()
9696
self.tags = tags or []
9797
self.latest_run: Optional[Run] = None
98+
self.run_has_token_event_map: dict[str, bool] = {}
9899

99100
def _start_trace(self, run: Run) -> None:
100101
if self.project_name:
@@ -235,6 +236,11 @@ def _llm_run_with_token_event(
235236
parent_run_id: Optional[UUID] = None,
236237
) -> Run:
237238
"""Append token event to LLM run and return the run."""
239+
run_id_str = str(run_id)
240+
if run_id_str not in self.run_has_token_event_map:
241+
self.run_has_token_event_map[run_id_str] = True
242+
else:
243+
return self._get_run(run_id, run_type={"llm", "chat_model"})
238244
return super()._llm_run_with_token_event(
239245
# Drop the chunk; we don't need to save it
240246
token,

0 commit comments

Comments
 (0)