Skip to content

Conversation

@hassiebp
Copy link
Contributor

@hassiebp hassiebp commented Feb 25, 2025

  • feat(client): add native environment support
  • add environment to client

Important

Add native environment support to Langfuse Python SDK, allowing users to specify environments for better telemetry data organization.

  • Behavior:
    • Added optional environment field to core models (Observation, Trace, BaseScore, ScoreBody) with validation for lowercase alphanumeric strings with hyphens/underscores.
    • Implemented environment parameter in Langfuse client constructor with support for setting via LANGFUSE_TRACING_ENVIRONMENT environment variable.
    • Added environment support to integration handlers (LangchainCallbackHandler, LlamaIndexInstrumentor, OpenAI integration).
    • Added comprehensive tests to verify environment propagation to traces, observations, and scores.

This description was created by Ellipsis for 8fd01cd. It will automatically update as commits are pushed.


Greptile Summary

Disclaimer: Experimental PR review

Added native environment support to the Langfuse Python SDK, allowing users to specify environments for better organization of telemetry data across different deployment contexts.

  • Added optional environment field to core models (Observation, Trace, BaseScore, ScoreBody) with validation for lowercase alphanumeric strings with hyphens/underscores
  • Implemented environment parameter in Langfuse client constructor with support for setting via LANGFUSE_TRACING_ENVIRONMENT environment variable
  • Added environment support to integration handlers (LangchainCallbackHandler, LlamaIndexInstrumentor, OpenAI integration)
  • Added comprehensive tests to verify environment propagation to traces, observations, and scores

@hassiebp hassiebp marked this pull request as ready for review February 26, 2025 14:34
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

19 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines 2167 to 2175
self.environment = environment or os.environ.get("LANGFUSE_TRACING_ENVIRONMENT")

if self.environment and not bool(
re.match(ENVIRONMENT_PATTERN, self.environment)
):
self.log.warning(
f'Invalid environment specified "{environment}" that does not match validation pattern ("{ENVIRONMENT_PATTERN}"). Setting will be ignored.'
)
self.environment = None
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: There's an inconsistency in error handling between the main Langfuse class and StatefulClient. The main class logs an error for invalid environments but still keeps the value, while StatefulClient logs a warning and sets the environment to None. These should be consistent.

):
self.log.error(
f'Invalid environment specified "{environment}" that does not match validation pattern ("{ENVIRONMENT_PATTERN}"). Events will be rejected by Langfuse servers.'
)
Copy link
Contributor

Choose a reason for hiding this comment

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

If an invalid environment is specified, consider resetting self.environment to None after logging the error to prevent subsequent events from carrying an invalid value.

Suggested change
)
self.environment = None

enabled: Enables or disables the Langfuse client. Defaults to True. If disabled, no observability data will be sent to Langfuse. If data is requested while disabled, an error will be raised.
mask (Callable): Function that masks sensitive information from input and output in log messages.
environment (optional): The tracing environment. Can be any lowercase alphanumeric string with hyphens and underscores that does not start with 'langfuse'. Can bet set via `LANGFUSE_TRACING_ENVIRONMENT` environment variable.
Copy link
Contributor

Choose a reason for hiding this comment

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

Typographical error: In the environment parameter description, change 'Can bet set via LANGFUSE_TRACING_ENVIRONMENT environment variable.' to 'Can be set via LANGFUSE_TRACING_ENVIRONMENT environment variable.'

Suggested change
environment (optional): The tracing environment. Can be any lowercase alphanumeric string with hyphens and underscores that does not start with 'langfuse'. Can bet set via `LANGFUSE_TRACING_ENVIRONMENT` environment variable.
environment (optional): The tracing environment. Can be any lowercase alphanumeric string with hyphens and underscores that does not start with 'langfuse'. Can be set via `LANGFUSE_TRACING_ENVIRONMENT` environment variable.

@hassiebp hassiebp merged commit 07a1993 into main Feb 28, 2025
7 of 10 checks passed
@hassiebp hassiebp deleted the hassieb/lfe-4084-add-env-property branch February 28, 2025 13:21
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