import os from langfuse.callback import CallbackHandler def create_langfuse_handler(*args, **kwargs) -> CallbackHandler: """ Create a Langfuse handler for tracking and logging. """ public_key = kwargs.get("public_key") or ( args[0] if len(args) > 0 else os.getenv("LANGFUSE_PUBLIC_KEY") ) secret_key = kwargs.get("secret_key") or ( args[1] if len(args) > 1 else os.getenv("LANGFUSE_SECRET_KEY") ) host = kwargs.get("host") or ( args[2] if len(args) > 2 else os.getenv("LANGFUSE_HOST", "https://api.langfuse.com") ) if not public_key: raise ValueError( "Please provide a public key or set the environment variable 'LANGFUSE_PUBLIC_KEY'." ) if not secret_key: raise ValueError( "Please provide a secret key or set the environment variable 'LANGFUSE_SECRET_KEY'." ) return CallbackHandler( public_key=public_key, secret_key=secret_key, host=host, **kwargs, )