Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(llm-observability): LangChain spans #176

Merged
merged 19 commits into from
Jan 28, 2025
Merged

Conversation

skoob13
Copy link
Contributor

@skoob13 skoob13 commented Jan 27, 2025

Problem

We want to capture the input and output of intermediary chains during agent workflows.

Changes

  • Add the spans support for the LangChain callback handler.
  • Replace the RunMetadata dict with dataclasses.
  • Fix tests.

New fields:

  • $ai_parent_id to capture a parent ID and build a tree.
  • $ai_generation_id to capture a generation ID and unify API.
  • $ai_span_id to capture an ID for a span.

@skoob13 skoob13 requested review from Twixes and k11kirky January 27, 2025 10:59
if not run:
return
if isinstance(run, GenerationMetadata):
log.warning(f"Run {run_id} is not a generation but attempted to be captured as a trace or span.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.warning(f"Run {run_id} is not a generation but attempted to be captured as a trace or span.")
log.warning(f"Run {run_id} is a generation, but attempted to be captured as a trace or span.")

run = self._pop_run_metadata(run_id)
if not run:
return
if not isinstance(run, GenerationMetadata):
log.warning(f"Run {run_id} is not a generation but attempted to be captured as a generation.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.warning(f"Run {run_id} is not a generation but attempted to be captured as a generation.")
log.warning(f"Run {run_id} is not a generation, but attempted to be captured as a generation.")

Comment on lines 443 to 448
if parent_run_id is not None:
event_properties["$ai_span_id"] = run_id
event_properties["$ai_parent_id"] = parent_run_id
event_properties["$ai_span_name"] = run.name
else:
event_properties["$ai_trace_name"] = run.name
Copy link
Member

@Twixes Twixes Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one thing that gets me, should we be using $ai_span_id and $ai_span_name for both span and trace IDs/names after all? It feels pretty intuitive and definitely much simpler than somewhat duplicated properties (it'd be a breaking change, but it's a beta product)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about unifying them, too. Intuitively, I want to inherit $ai_generation and $ai_trace from $ai_span, so it makes sense to use $ai_span_id and $ai_span_name on all LLM events. We still want to keep $ai_trace_id and $ai_parent_id, though.

So I propose renaming $ai_trace_name (existing) to $ai_span_name, keeping the naming unified across all three events.

Copy link
Contributor Author

@skoob13 skoob13 Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@skoob13 skoob13 requested a review from Twixes January 27, 2025 19:53
Copy link
Member

@Twixes Twixes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM



RunStorage = Dict[UUID, RunMetadata]
RunMetadataUnion = Union[SpanMetadata, GenerationMetadata]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: A little redundant to suffix with Union

Suggested change
RunMetadataUnion = Union[SpanMetadata, GenerationMetadata]
RunMetadata = Union[SpanMetadata, GenerationMetadata]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, I've missed that. There was a naming conflict before haha. I'll fix it!

@skoob13 skoob13 merged commit 8f43bbc into master Jan 28, 2025
2 checks passed
@skoob13 skoob13 deleted the feat/langchain-spans branch January 28, 2025 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants