-
Notifications
You must be signed in to change notification settings - Fork 10
fix(templates): map LITELLM_API_KEY to OPENAI_API_KEY in Temporal workers #514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
michaelxu2288
wants to merge
6
commits into
scaleapi:next
Choose a base branch
from
michaelxu2288:fix/templates-temporal-openai-key
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0fa93b6
codegen metadata
stainless-app[bot] 76252a9
feat(tracing): add opt-in commit SHA stamping for SGP spans (#505)
cyntwang99 f394ce7
codegen metadata
stainless-app[bot] 0db6037
fix: keep agent output streaming alive on an unreadable line, and hon…
chakrris 8ad88db
fix(templates): map LITELLM_API_KEY to OPENAI_API_KEY in Temporal wor…
michaelxu2288 b5f5e05
fix(templates): load the project .env in the Temporal worker before m…
michaelxu2288 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| configured_endpoints: 75 | ||
| openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/sgp/agentex-sdk-7acaeb315af90255109ae17afc71e32a8e5851bb8a956a2a284cb4d344dfab51.yml | ||
| openapi_spec_hash: 3044e94b48d60311b6048e8df88e7552 | ||
| openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/sgp/agentex-sdk-ee0c521f0612c31b874bd595b90cd9209545bab603983552a1a7a87f38ed931e.yml | ||
| openapi_spec_hash: 917a1ffe9e353bed2740524dec786ed2 | ||
| config_hash: 593e89b291976a5e84e4c3c3f8324354 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| """Opt-in stamping of the agent's source commit onto its spans. | ||
|
|
||
| Nothing is stamped until the agent calls :func:`enable`, mirroring the | ||
| ``lineage`` registry next door: a process-wide switch the agent sets once at | ||
| import, rather than automatic behaviour every agent inherits. When enabled the | ||
| resolved commit lands in span data under ``__commit_sha__`` and is searchable in | ||
| the SGP Traces UI as ``__commit_sha__:<sha>``. | ||
|
|
||
| This is deliberately separate from ``__agent_version__``, which is automatic and | ||
| carries the deployed image tag verbatim ("image tag or git sha"). That tag is a | ||
| real commit on some build paths but an ``<image-name>-<sha>`` composite (AWS | ||
| ECR), ``latest``, or a hand-passed tag on others -- so a field named for a commit | ||
| must not simply mirror it. Values that are not git object names are refused, and | ||
| a field named ``__commit_sha__`` therefore only ever holds one. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import re | ||
|
|
||
| from agentex.lib.utils.logging import make_logger | ||
|
|
||
| __all__ = ("COMMIT_SHA_KEY", "enable", "disable", "is_enabled", "commit_sha") | ||
|
|
||
| logger = make_logger(__name__) | ||
|
|
||
| COMMIT_SHA_KEY = "__commit_sha__" | ||
|
|
||
| # A git object name: 40 hex for SHA-1, 64 for SHA-256, or an abbreviation down to | ||
| # git's own 7-character minimum. | ||
| _GIT_SHA_RE = re.compile(r"[0-9a-fA-F]{7,64}") | ||
|
|
||
| _COMMIT_SHA_ENV = "AGENT_COMMIT_SHA" | ||
| # Fallback only: automatic, and only usable when it happens to be SHA-shaped. | ||
| _AGENT_VERSION_ENV = "AGENT_VERSION" | ||
|
|
||
| # Resolved once at enable() rather than per span: the value is fixed for the | ||
| # life of the process, and resolving eagerly means a bad value is reported at | ||
| # startup instead of silently producing unstamped spans. | ||
| _commit_sha: str | None = None | ||
|
|
||
|
|
||
| def enable(commit_sha: str | None = None) -> None: | ||
| """Opt this process in to stamping ``__commit_sha__`` onto every span. | ||
|
|
||
| Value precedence: the explicit ``commit_sha`` argument, else | ||
| ``AGENT_COMMIT_SHA``, else ``AGENT_VERSION`` when the deployment happened to | ||
| set it to a bare commit SHA. A value that is not a git object name is | ||
| refused with a warning and leaves stamping off -- better an absent field | ||
| than one named for a commit that holds an image tag. | ||
| """ | ||
| global _commit_sha | ||
|
|
||
| for value, source in ( | ||
| (commit_sha, "the commit_sha argument"), | ||
| (os.environ.get(_COMMIT_SHA_ENV), _COMMIT_SHA_ENV), | ||
| (os.environ.get(_AGENT_VERSION_ENV), _AGENT_VERSION_ENV), | ||
| ): | ||
| candidate = (value or "").strip() | ||
| if not candidate: | ||
| continue | ||
| if _GIT_SHA_RE.fullmatch(candidate): | ||
| _commit_sha = candidate | ||
| logger.info("code revision stamping enabled from %s", source) | ||
| return | ||
| # An explicit argument or AGENT_COMMIT_SHA is a direct statement of | ||
| # intent, so a bad value there is worth surfacing. AGENT_VERSION is only | ||
| # a fallback and is expected to be a non-SHA tag much of the time, so | ||
| # falling through it quietly is correct, not a silent failure. | ||
| if source != _AGENT_VERSION_ENV: | ||
| logger.warning( | ||
| "%s=%r is not a git commit SHA; __commit_sha__ will not be stamped.", | ||
| source, | ||
| candidate, | ||
| ) | ||
| _commit_sha = None | ||
| return | ||
|
|
||
| _commit_sha = None | ||
| logger.warning( | ||
| "code revision stamping was enabled but no commit SHA was found " | ||
| "(checked the commit_sha argument, %s, and %s); __commit_sha__ will not " | ||
| "be stamped. Set %s in the agent's environment -- e.g. bake it at build " | ||
| "time with a Dockerfile ARG/ENV.", | ||
| _COMMIT_SHA_ENV, | ||
| _AGENT_VERSION_ENV, | ||
| _COMMIT_SHA_ENV, | ||
| ) | ||
|
|
||
|
|
||
| def disable() -> None: | ||
| """Turn stamping back off (also used for test isolation).""" | ||
| global _commit_sha | ||
| _commit_sha = None | ||
|
|
||
|
|
||
| def is_enabled() -> bool: | ||
| """Whether a commit SHA resolved and will be stamped.""" | ||
| return _commit_sha is not None | ||
|
|
||
|
|
||
| def commit_sha() -> str | None: | ||
| """The resolved commit SHA, or ``None`` when stamping is not enabled.""" | ||
| return _commit_sha |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new mapping runs before the local worker loads the generated project’s
.env. The currentagents runpath builds the worker environment from the parent process and manifest values, whileEnvironmentVariables.refresh()runs later and does not load the project’s.env. As a result, following the template with onlyLITELLM_API_KEYin.envleaves_litellm_keyunset, and the worker still reaches the OpenAI client withoutOPENAI_API_KEY, causing the missing-credentials failure. The Pydantic AI worker template has the same issue. This fix therefore depends on the separate CLI environment-loading change and does not work if merged or released alone.Knowledge Base Used: Command-line workflows
Prompt To Fix With AI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, and that is exactly the root cause I hit live: nothing loads the project
.envfor the worker today, so the mapping only helped when some import (litellm) happened to callload_dotenv()first. Two changes: this PR now callsload_dotenv()at the top of both worker templates before the mapping (b5f5e05, standalone), and #515 makesagents runload the project.envinto both subprocess environments so it no longer depends on import order. Verified live on scaffolded Temporal + OpenAI Agents and Temporal + Pydantic AI agents with onlyLITELLM_API_KEYin.env.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved. The latest commit loads the project
.envviaload_dotenv()before theLITELLM_API_KEY→OPENAI_API_KEYmapping and before any project/framework imports in both worker templates. That makes this PR standalone, while #515 additionally ensures the loaded environment is passed into both subprocesses. The mapping preserves an explicitly configuredOPENAI_API_KEY, so no further change is needed for this finding.Tip: You can customize Greptile's behavior for this repo with
.greptile/rules.mdand.greptile/config.json.