Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

GoogleGcpCredentials: Enforce dictionary type

未关闭
#1,207 0 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

@vprashrex 已经在做这个了。

开始于 2026年9月23日。

评估

这个 Issue 还没有评估数据。

描述

Is your feature request related to a problem?
The current typing of GoogleGcpCredentials.sa_key as JsonValue allows for string inputs, leading to runtime failures when the service account key is treated as a string instead of a dictionary. This results in high diagnostic costs and failed batch assessments.

Describe the solution you'd like
Narrow the annotation of sa_key to enforce a dictionary type:

  • Change sa_key: JsonValue to sa_key: dict[str, JsonValue]
  • This will ensure validation fails at credential-creation time with a clear error, eliminating the need for casting in the code.
  • Note that existing string inputs will need to be re-saved as JSON objects to avoid issues.
Original issue

Problem

GoogleGcpCredentials.sa_key is typed JsonValue:

# backend/app/core/providers.py:76
sa_key: JsonValue = Field(description="Service account key JSON")

Pydantic's JsonValue is the union of all JSON value types — str included. A service account key submitted as an escaped JSON string therefore passes validation and is persisted as-is.

The failure surfaces much later, at provider runtime:

# backend/app/core/batch/google_gcp.py:91
creds = build_gcp_sa_credentials(cast(dict[str, Any], creds_model.sa_key))
# backend/app/core/cloud/storage.py:516
def build_gcp_sa_credentials(sa_key: dict[str, Any]) -> service_account.Credentials:
    return service_account.Credentials.from_service_account_info(sa_key, scopes=list(GCS_SCOPES))

from_service_account_info calls .keys() on the argument, so a stored string raises:

'str' object has no attribute 'keys'

Impact

A batch assessment run fails with status: FAILED and error: "'str' object has no attribute 'keys'", after all items have already been dispatched. Observed with 9/9 items returning assessment: null. The message gives no hint that the stored credential is malformed, so the cost of diagnosis is high.

The cast(dict[str, Any], ...) at the call site also hides this from pyright — the cast asserts a shape the type system never guaranteed.

Proposed fix

Narrow the annotation so the type system enforces what the consumer requires:

sa_key: dict[str, JsonValue] = Field(description="Service account key JSON")

Validation then fails at credential-creation time with a clear Pydantic error instead of at run time. The cast in google_gcp.py becomes unnecessary and should be dropped.

Notes

  • Existing rows holding a string sa_key will not be repaired by the type change; they need to be re-saved (or backfilled) with the key as a JSON object.
  • Masking in mask_credentials (providers.py:260) already assumes non-string secrets for sa_key, so the dict shape is the one the rest of the code expects.
主要语言
Python
星标
18
派生
10
平均合并
2 天 23 小时
30 天内合并 PR
13

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ProjectTech4DevAI/kaapi-backend 的其他 Issue

查看 ProjectTech4DevAI/kaapi-backend 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。