Repository navigation
feat(client): support organization API keys with api_key and project_id - #2074
marksalpeter wants to merge 9 commits into
Conversation
Organization API keys can write to any project in their organization, so each request has to name its target project in the x-langfuse-project-id header. Add a project_id argument (env LANGFUSE_PROJECT_ID) to Langfuse() and identify a client by (public_key, project_id) everywhere: - LangfuseResourceManager instances are keyed by the pair, so a second client on the same org key for another project no longer silently reuses the first client's setup. - Tracer scope attributes carry project_id and LangfuseSpanProcessor filters on both, so two clients sharing an org key never export each other's spans. - get_client() and @observe accept project_id / langfuse_project_id; an org key shared by several clients without a project_id resolves to a disabled client. The header is sent on OTLP exports and on the generated API clients (so a caller-provided httpx_client sends it too). A conflicting project header in additional_headers or httpx_client.headers raises ValueError at init. A configured project_id is used for trace and experiment URLs without calling projects.get(). A custom span_exporter with project_id warns that the exporter must set the header, and a 403 span export logs a one-time hint to set LANGFUSE_PROJECT_ID. Project API keys leave project_id unset, so their behavior is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016PExPf2jnm8CPvdxKYmfqt
|
Resolved: the CLA is now signed ( Generated by Claude Code |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016PExPf2jnm8CPvdxKYmfqt
Add an api_key argument and LANGFUSE_API_KEY env var for Langfuse API keys (lf_<cuid>). They authenticate as a bearer token, replace the public/secret key pair, and require project_id. The key format is checked locally, so a key pair value passed as api_key (or the reverse) fails at construction with instructions. API key clients have no public key: get_client(project_id=...) and @observe(langfuse_project_id=...) select them, while key pair clients keep being selected by public key. Send the project as langfuse-project-id, the header the server's authorization layer reads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016PExPf2jnm8CPvdxKYmfqt
|
E2E shard 2 tests on Python 3.13 fails intermittently in 4 tests in
Each one fails on Root cause: a startup race in the server (langfuse/langfuse). I reproduced it locally with CI's compose file and the image tagged
Verified:
Fix: the worker's price seeding should wait for migrations or retry; that belongs in langfuse/langfuse. Until then, this repo's CI could restart Generated by Claude Code |
Key pairs belong to one project, so an explicit project_id with public_key/secret_key now raises ValueError, and key pair clients ignore LANGFUSE_PROJECT_ID. A client is then selected by exactly one identifier: project_id for API key clients, public_key for key pairs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016PExPf2jnm8CPvdxKYmfqt
Mark public_key/secret_key and their env vars as deprecated in favor of api_key, shorten the api_key/project_id docs, and validate project headers inside _get_credentials. Drop the get_client branch for several matches, which one identifier per client makes unreachable. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016PExPf2jnm8CPvdxKYmfqt
…-sdks-to-support-multi-org-api-tokens
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016PExPf2jnm8CPvdxKYmfqt
AI review gate — Draft pull request — AI review not started
What to do:
Configuration: the repository's History (latest 5 heads, newest first)
|
…angChain Add langfuse_project_id to OpenAI calls and project_id to the LangChain CallbackHandler, so API key clients can be selected outside @observe. get_client() now warns and returns a disabled client when given both public_key and project_id. A credential misconfiguration found while get_client() creates the default client is logged instead of raised, so integrations never fail the user's call; Langfuse(...) still raises. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016PExPf2jnm8CPvdxKYmfqt
Per the revised RFC, key formats are interchangeable: an existing sk-lf- secret works as api_key (bearer with project_id), and a new key works as secret_key with the project ID as public_key (Basic auth), so older SDKs can use new keys. Drop the lf_ prefix checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016PExPf2jnm8CPvdxKYmfqt
What does this PR do?
Implements the Python side of "RFC: SDK support for organization API keys" (LFE-18172). Organization and user API keys can write to many projects, so each client names its target project. The change only adds behavior: existing key-pair setups work as before.
New configuration:
Langfuse(api_key=..., project_id=...), or theLANGFUSE_API_KEYandLANGFUSE_PROJECT_IDenv vars.Authorization: Bearer <api_key>on OTLP exports and on the generatedLangfuseAPI/AsyncLangfuseAPIclients.sk-lf-...secret works asapi_key.secret_keywith the project ID aspublic_key, over Basic auth. That's how older SDK versions use new keys.Langfuse(...)raisesValueErrorfor:api_keycombined withpublic_key/secret_key;api_keywithoutproject_id;project_idpassed with a key pair.LANGFUSE_PROJECT_ID, so one process can mix both kinds of client.public_key/secret_keyarguments take precedence overLANGFUSE_API_KEY.Project header: API key clients send
langfuse-project-idon every API request and span export. The server accepts it with or without thex-prefix. Ifadditional_headersorhttpx_client.headerssets either header name to a different project, init raisesValueError.Routing: each client is selected by exactly one identifier.
api_key+project_idget_client(project_id=...),langfuse_project_id=...on@observefunctions and OpenAI calls,CallbackHandler(project_id=...)public_key+secret_keyget_client(public_key=...),langfuse_public_key=...,CallbackHandler(public_key=...), as today@observecall.TracerProviderexport only their own spans.No breaking changes in integrations:
get_client()creates the default client when none exists. If that hits a credential misconfiguration, it logs the error and returns a disabled client instead of raising inside an OpenAI or LangChain call.No project lookup: API key clients return the configured
project_idfrom_get_project_id()without callingprojects.get(). Trace and experiment URLs use it.Hints:
project_id.project_idcombined with a customspan_exporterlogs a warning that the exporter must send the header itself.Notes for reviewers
langfuse-project-id. On langfusemain,Bearerstill means a public key with scores-only access. Theapi_keypath is covered by unit tests and works end to end once the server ships it.api_key: two clients reuse one instance, and the first key wins. That matches how the SDK already treats a reused public key.logging.Filteron the OTLP exporter's logger, because the exporter doesn't return the status code. It never suppresses the record.total_costassertions intests/e2e/test_decorators.pyfail.mainfails the same way.Type of change
Verification
tests/unit/test_prompt.pyerror locally becauseLANGFUSE_PUBLIC_KEYisn't set; they error the same way onmain.1f606ce3) against a local server (CI's compose file, image4= v4.86.0) with seeded model prices. These exercise the key-pair path, which the latest commit doesn't touch: it only removes the key-format checks.Checklist
code_review.md..env.templateif needed.🤖 Generated with Claude Code
https://claude.ai/code/session_016PExPf2jnm8CPvdxKYmfqt