openllmetry icon indicating copy to clipboard operation
openllmetry copied to clipboard

πŸš€ Feature: Emit events for langchain state, tool call input/output

Open josephwangrb opened this issue 1 month ago β€’ 1 comments

Which component is this feature for?

Langchain Instrumentation

πŸ”– Feature description

Emit events to capture chain level and tool call input&output.

🎀 Why is this feature needed ?

Currently, the OpenLLMetry LangChain instrumentation captures spans for chain and tool executions, including metadata such as input/output and timing information. However, it does not emit events for the input/output corresponding to key lifecycle points such as:

  • Chain start

  • Chain end

  • Tool start

  • Tool end

I noticed that we did add input/output as span attributes, but this logic should also emit them as events when should_emit_events return true? refer to: https://github.com/traceloop/openllmetry/blob/main/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py#L420

✌️ How do you aim to achieve this?

When should_emit_events return true and should_send_prompts returns true, we emit input/output as events.

For example

        if should_emit_events() and should_send_prompts():
            emit_event(
                ToolEndEvent(
                    message=json.dumps(
                        {"outputs": outputs, "kwargs": kwargs},
                        cls=CallbackFilteredJSONEncoder,
                    ),
                )

πŸ”„οΈ Additional Information

It looks like the OpenTelemetry GenAI semantic conventions don’t currently define events for chain or tool lifecycle stages. Should OpenLLMetry aim to strictly follow the spec, or would it make sense to introduce a few practical extensions like this?

πŸ‘€ Have you spent some time to check if this feature request has been raised before?

  • [x] I checked and didn't find similar issue

Are you willing to submit PR?

Yes I am willing to submit a PR!

josephwangrb avatar Nov 05 '25 15:11 josephwangrb

A summary of the changes CodeRabbit can apply:

  • Implement LangChain chain/tool lifecycle event support by adding four event dataclasses in packages/opentelemetry-instrumentation-langchain/.../event_models.py, extending event_emitter.py with four new emitters and updated emit_event handling for "langchain.chain." and "langchain.tool.", and updating callback_handler.py to emit chain/tool start/end events while preserving legacy span-attribute behavior.

  • Add new ChainStart/ChainEnd and ToolStart/ToolEnd event dataclasses to event_models, extend event_emitter to support emitting those new events (with prompt-filtering logic and new event names), and update callback_handler to emit these chain/tool start/end events (and adjust span-attribute behavior accordingly) across the three modified files.

  • [ ] βœ… Create PR with these edits
  • [ ] πŸ“‹ Get copyable edits

coderabbitai[bot] avatar Nov 05 '25 15:11 coderabbitai[bot]