Spaces:
Running
Running
Fix LangSmith env vars: use modern LANGSMITH_* naming per docs
Browse files4 unmodified lines5678910111225 unmodified lines383940414243444546474849505152535455565758596061626364656667684 unmodified linesmetadata attribution, health checks, and alerting concerns.Backends--------* **LangSmith** β automatic via ``LANGCHAIN_API_KEY`` / ``LANGCHAIN_PROJECT``env vars. LangGraph/LangChain reads these at import time and startstracing without any per-call code.* **Langfuse** β explicit ``CallbackHandler`` instantiated only when25 unmodified linesdef is_langsmith_enabled() -> bool:"""Return True if LangSmith tracing is configured via env vars."""return bool(os.environ.get("LANGCHAIN_API_KEY", "").strip())def enable_langsmith(api_key: str, project: str = "") -> None:"""Set the environment variables that LangChain/LangGraph reads at runtime.Call this at startup **before** any LangChain/LangGraph imports so thatthe tracing is active from the first call."""os.environ["LANGCHAIN_TRACING_V2"] = "true"os.environ["LANGCHAIN_API_KEY"] = api_keyif project:os.environ["LANGCHAIN_PROJECT"] = projectlogger.info("LangSmith tracing enabled (project=%s)",project or os.environ.get("LANGCHAIN_PROJECT", "default"),)def disable_langsmith() -> None:"""Remove LangSmith env vars so tracing stops."""os.environ.pop("LANGCHAIN_TRACING_V2", None)os.environ.pop("LANGCHAIN_API_KEY", None)# Keep LANGCHAIN_PROJECT in case user wants to set it separately# βββ Langfuse (explicit callback) ββββββββββββββββββββββββββββββββββββββββββ4 unmodified lines5678910111225 unmodified lines383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788894 unmodified linesmetadata attribution, health checks, and alerting concerns.Backends-------- * **LangSmith** β automatic via ``LANGSMITH_API_KEY`` / ``LANGSMITH_PROJECT``* (also accepts legacy ``LANGCHAIN_API_KEY`` for back-compat)env vars. LangGraph/LangChain reads these at import time and startstracing without any per-call code.* **Langfuse** β explicit ``CallbackHandler`` instantiated only when25 unmodified linesdef is_langsmith_enabled() -> bool:"""Return True if LangSmith tracing is configured via env vars.Checks both the new ``LANGSMITH_API_KEY`` (preferred) and legacy``LANGCHAIN_API_KEY`` env vars for backward compatibility."""return bool(os.environ.get("LANGSMITH_API_KEY", "").strip()or os.environ.get("LANGCHAIN_API_KEY", "").strip())def enable_langsmith(api_key: str, project: str = "") -> None:"""Set the environment variables that LangSmith reads at runtime.Per the official LangSmith docs (https://docs.langchain.com/langsmith/trace-with-langgraph):the modern env vars are ``LANGSMITH_TRACING`` and ``LANGSMITH_API_KEY``.We also set the legacy ``LANGCHAIN_*`` vars for backward compatibilitywith older LangChain versions.Call this at startup **before** any LangChain/LangGraph imports so thatthe tracing is active from the first call."""# Modern env vars (required by langsmith-sdk >= 0.7)os.environ["LANGSMITH_TRACING"] = "true"os.environ["LANGSMITH_API_KEY"] = api_key# Legacy env vars (backward compat with older LangChain)os.environ["LANGCHAIN_TRACING_V2"] = "true"os.environ["LANGCHAIN_API_KEY"] = api_keyif project:os.environ["LANGSMITH_PROJECT"] = projectos.environ["LANGCHAIN_PROJECT"] = projectlogger.info("LangSmith tracing enabled (project=%s)",project or os.environ.get("LANGSMITH_PROJECT", "default"),)def disable_langsmith() -> None:"""Remove LangSmith env vars so tracing stops."""# Modern varsos.environ.pop("LANGSMITH_TRACING", None)os.environ.pop("LANGSMITH_API_KEY", None)# Legacy varsos.environ.pop("LANGCHAIN_TRACING_V2", None)os.environ.pop("LANGCHAIN_API_KEY", None)# Keep project name in case user wants to set it separately# βββ Langfuse (explicit callback) ββββββββββββββββββββββββββββββββββββββββββ
observability.py+28-7