fix(span): fallback to client environment if not provided #1304
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes langfuse/langfuse#8619
Another approach would be passing the environment starting from the call to
LangfuseGeneration
instart_generation
. Not sure which one is the best fixImportant
In
LangfuseSpanWrapper.__init__()
, fallback tolangfuse_client._environment
ifenvironment
is not provided.LangfuseSpanWrapper.__init__()
, set_environment
toenvironment
or fallback tolangfuse_client._environment
if not provided.This description was created by
for a197b67. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Summary
This PR fixes a bug where span wrappers were losing their environment context when no environment parameter was explicitly provided during span creation. The change implements a simple fallback mechanism in the
Span
constructor at line 111, changing fromself._environment = environment
toself._environment = environment or langfuse_client._environment
.The issue occurred because the Langfuse client stores its environment setting in the
_environment
attribute (as configured via constructor or environment variables likeLANGFUSE_TRACING_ENVIRONMENT
), but when creating spans in a trace hierarchy, this environment wasn't being automatically inherited by child spans unless explicitly passed down through all method calls.This fix ensures that all spans within a trace maintain consistent environment settings by falling back to the client's configured environment when none is explicitly provided. This is critical for proper trace organization in Langfuse, as the environment field is used for categorizing and filtering traces in the UI. The solution is minimal and non-breaking - it only affects spans where no environment is explicitly set, maintaining backward compatibility while fixing the inheritance issue.
The change integrates well with the existing codebase architecture where the client acts as the central configuration holder, and spans should naturally inherit these settings unless overridden.
Confidence score: 5/5