Skip to content

Conversation

cstub
Copy link
Member

@cstub cstub commented May 15, 2025

  • Integrate Langfuse SDK to trace
    • Agent turns
    • Model calls
    • Code executions
  • Create tracing package containing public API and implementation
  • Adapt quickstart to use tracing
  • Add tracing to CLI
  • Add observability documentation
  • TODO: API docs and screenshots for observability documentation is missing

cstub added 3 commits May 14, 2025 11:13
* Integrate Langfuse SDK to trace
  - Agent runs
  - Model calls
  - Code executions
* Create `tracing` package containing
  public API and implemenation
* Adapt quickstart to use tracing
* Add tracing to CLI
* Use `tracing.configure()` instead of a tracing context manager
* Add observability documentation
* TODO: API docs and screenshots for observability documentation
  is missing
@cstub cstub force-pushed the wip-tracing-new branch from 7462083 to 3b41507 Compare May 15, 2025 19:43
- async tracer API
- async langfuse impl
- tracing data created in stream() methods of
  - CodeActAgentTurn
  - LiteLLMTurn
- renamings
@krasserm
Copy link
Member

@cstub I experimented with some changes here for further discussion. See commit message for a high-level overview of changes.

Regarding the async tracer API, I'm aware, that the langfuse Python SDK doesn't block for writing tracing data i.e.. uses a queue which shouldn't be an issue for async client code, but they still use a sync logger API which may cause troubles with some logging backends. I didn't look further into the langfuse async client. Seems to be even more lower-level, but this might be an alternative for a later change.

I'll take a look at the user docs in a next step.

@krasserm
Copy link
Member

@cstub I also propose some changes to the documentation, to be discussed of course.

@krasserm krasserm requested a review from Copilot May 20, 2025 07:46
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR integrates the Langfuse SDK to enable tracing for agent turns, model calls, and code executions, while introducing a new tracing package and updating documentation and CLI configuration accordingly.

  • Added test cases for tracing context, agent turns, and model interactions.
  • Implemented new tracing classes (Langfuse, Noop, context) and updated the agent, model, CLI, and docs to support observability.

Reviewed Changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/unit/tracing/* Added tests to validate tracing configuration and behavior.
freeact/tracing/* New tracing package with Langfuse and Noop implementations.
freeact/model/litellm.py Modified to create spans and update trace data during streaming.
freeact/agent.py Updated to include trace creation, span management for code execution and agent run.
pyproject.toml, mkdocs.yml, docs/* Updated configuration and documentation for observability integration.

Copy link
Member

@krasserm krasserm left a comment

Choose a reason for hiding this comment

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

No comments on the current changes in the PR, looks very good! However, I found an issue while experimenting with the observability.py example.

To reproduce:

Set model_name=gpt-4.1 and leave all other settings as-is. This will cause an error in litellm because gpt-4.1 doesn't support a reasoning effort which is expected. However, what I see first in the stack trace is

Task exception was never retrieved
future: <Task finished name='Task-170' coro=<<async_generator_athrow without __name__>()> exception=ValueError("<Token var=<ContextVar name='tracing_active_trace' default=None at 0x73b74d6fc310> at 0x73b74c2c7340> was created in a different Context")>
Traceback (most recent call last):
  File "/home/martin/Development/gradion/freeact/freeact/tracing/context.py", line 149, in trace
    yield active_trace
  File "/home/martin/Development/gradion/freeact/freeact/agent.py", line 94, in stream
    yield elem
GeneratorExit

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/martin/Development/gradion/freeact/freeact/agent.py", line 82, in stream
    async with tracing.trace(
  File "/home/martin/Applications/miniconda3/envs/gradion-freeact/lib/python3.11/contextlib.py", line 231, in __aexit__
    await self.gen.athrow(typ, value, traceback)
  File "/home/martin/Development/gradion/freeact/freeact/tracing/context.py", line 151, in trace
    _active_trace_context.reset(token)
ValueError: <Token var=<ContextVar name='tracing_active_trace' default=None at 0x73b74d6fc310> at 0x73b74c2c7340> was created in a different Context

This is unexpected. Can you please investigate that?

@krasserm
Copy link
Member

I could fix the problem mentioned in this comment. asyncio seems to run the finalizer after an exception in a different task than the code that caused the exception, hence the "different context" error. This is now fixed in this commit. It also calls trace/span end() within the context manager which is what should be expected from the context manager anyway.

Copy link
Member

@krasserm krasserm left a comment

Choose a reason for hiding this comment

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

LGTM!

@krasserm krasserm merged commit 2ef3e77 into main May 21, 2025
9 checks passed
@krasserm krasserm deleted the wip-tracing-new branch May 21, 2025 06:07
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