Skip to content

🐛 migrate langfuse v3 API to v4, remove type: ignore#151

Merged
Miyamura80 merged 2 commits intomainfrom
fix/langfuse-v4-type-ignore
Mar 30, 2026
Merged

🐛 migrate langfuse v3 API to v4, remove type: ignore#151
Miyamura80 merged 2 commits intomainfrom
fix/langfuse-v4-type-ignore

Conversation

@Miyamura80
Copy link
Copy Markdown
Owner

Summary

  • Migrate from removed Langfuse v3 methods (.generation(), .span()) to v4 start_observation() API with as_type parameter
  • Fix .end() calls that passed kwargs no longer accepted in v4 — split into .update() + .end()
  • Import TraceContext TypedDict for proper trace_id/parent_span_id handling
  • Tighten current_tool_span type from Any | None to LangfuseSpan | None
  • Remove all type: ignore[attr-defined] suppressions — make ci passes with zero errors

Test plan

  • Verify make ci passes (ruff, vulture, ty, import lint, docs lint)
  • Integration test with live Langfuse: trigger an LLM call and confirm generation spans appear correctly
  • Integration test with live Langfuse: trigger a tool call and confirm tool spans appear correctly

🤖 Generated with Claude Code

Langfuse v4 removed `.generation()` and `.span()` from the client and
changed `.end()` to only accept `end_time`. Migrate to
`start_observation()` with `as_type` and split `.end()` into
`.update()` + `.end()`. Also tighten `current_tool_span` type from
`Any` to `LangfuseSpan`.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 30, 2026

Greptile Summary

This PR migrates utils/llm/dspy_langfuse.py from the now-removed Langfuse v3 low-level APIs (.generation(), .span()) to the v4 start_observation() method with the as_type parameter, and fixes the breaking change where .end() no longer accepted keyword arguments by splitting finalization into .update() + .end().

Key changes:

  • self.langfuse.generation(...)self.langfuse.start_observation(as_type="generation", ...) in on_lm_start
  • self.langfuse.span(...)self.langfuse.start_observation(as_type="tool", ...) in on_tool_start, using the semantically correct "tool" type
  • TraceContext TypedDict is now used to correctly pass trace_id and (conditionally) parent_span_id; parent_span_id is only included when it is truthy, avoiding invalid None entries
  • span.end(**kwargs)span.update(**kwargs); span.end() in both on_lm_end and on_tool_end
  • current_tool_span narrowed from ContextVar[Any | None] to ContextVar[LangfuseTool | None], matching the actual return type of start_observation(as_type="tool")
  • All type: ignore[attr-defined] suppressions removed

Confidence Score: 5/5

  • Safe to merge — straightforward API migration with no logic changes beyond adapting to the v4 interface.
  • All changes are mechanical API migrations. The TraceContext construction correctly guards parent_span_id behind a truthiness check, the update/end split matches v4 semantics, and the type tightening is accurate. No new conditionals, no removed guards, and the PR description confirms make ci (including type checking) passes cleanly.
  • No files require special attention.

Important Files Changed

Filename Overview
utils/llm/dspy_langfuse.py Migrates from removed Langfuse v3 .generation()/.span() methods to v4 start_observation(), splits .end(**kwargs) into .update() + .end(), imports TraceContext TypedDict, and tightens the current_tool_span type from Any to LangfuseTool. No logic issues found.

Reviews (2): Last reviewed commit: "🐛 address PR review feedback: use as_ty..." | Re-trigger Greptile

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@Miyamura80 Miyamura80 merged commit 9a307ec into main Mar 30, 2026
11 checks passed
@github-actions github-actions bot deleted the fix/langfuse-v4-type-ignore branch March 30, 2026 23:03
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.

1 participant