Skip to content

fix(observe): detect methods by first parameter only - #2055

Open
zichen0116 wants to merge 1 commit into
langfuse:mainfrom
zichen0116:fix/observe-is-method-first-param
Open

zichen0116 wants to merge 1 commit into
langfuse:mainfrom
zichen0116:fix/observe-is-method-first-param

Conversation

@zichen0116

@zichen0116 zichen0116 commented Oct 23, 2026 •

Copy link
Copy Markdown

Problem

@observe(capture_input=True) silently drops the first positional argument of a free function whenever any parameter is named self or cls — including keyword-only ones:

@observe(capture_input=True)
def classify(value, *, cls="news"): ...

classify("hello", cls="news")
# captured input: {"args": (), "kwargs": {"cls": "news"}}  -- "hello" is gone

The function itself still receives the argument and returns the correct value; only the traced input is wrong. Both sync and async functions are affected.

Root cause

LangfuseDecorator._is_method in langfuse/_client/observe.py treated a function as a method if "self" or "cls" appeared anywhere in the signature:

return (
    "self" in inspect.signature(func).parameters
    or "cls" in inspect.signature(func).parameters
)

_get_input_from_func_args then strips func_args[0] as the implicit receiver — which doesn't exist for free functions.

Fix

Only the first parameter indicates an unbound instance/class method, so _is_method now checks just that:

first_parameter = next(iter(inspect.signature(func).parameters), None)
return first_parameter in ("self", "cls")

Genuine instance methods (def m(self, ...)) and unbound classmethods (def m(cls, ...)) keep their existing behavior; bound methods never reach this code with self/cls visible (inspect.signature already drops the bound argument), so there is no behavior change there.

Verification

  • The issue's self-contained reproducer fails on main (args captured as ()) and passes with this fix for both sync and async paths.
  • Added 5 regression tests in tests/unit/test_observe.py:
    • free function with keyword-only cls keeps its first positional arg (sync + async),
    • instance method still drops self,
    • classmethod still drops cls,
    • free function with a later positional self is not treated as a method.
  • tests/unit/test_observe.py: 20/20 passed.
  • ruff check, ruff format --check, and mypy are clean on the changed files.

Closes #18859

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge.

Summary

Fixes @observe dropping the first argument of free functions that have a later parameter named self or cls.

  • Observed free functions keep their first argument in captured input.

No actionable issues found. Tests were not run during this review.

Reviews (1) · Last reviewed commit: "fix(observe): detect methods by first pa..." · Reviewed by Greptile

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

This branch has not been deployed

No deployments
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