Skip to main content

AgentGrid Python SDK

AgentGrid Python SDK v1 提供面向 Runtime 的高层调用接口,主路径围绕以下 API:

  • from agentgrid import AgentGridProfile
  • runtime.invoke()
  • runtime.create_runtime()/update_runtime()/delete_runtime()/get_runtime()/list_runtimes()
  • runtime_session()

Python 版本要求

  • Python >=3.10

安装

pip

pip install jdcloud-agentgrid

uv(下游项目)

uv add jdcloud-agentgrid
uv sync

SDK 默认配置文件(agentgrid.yaml)

get_sdk_default_config() 会自动尝试读取入口脚本同级的 agentgrid.yaml。

  • 查找规则固定为:Path(sys.argv[0]).resolve().parent / "agentgrid.yaml"
  • 文件不存在:忽略并回退到 SDK 内置默认配置
  • 仅识别 basic 节点,且字段必须是 AgentGridConfig 已定义字段

最小示例:

basic:
  region: cn-north-1
  endpoint: https://agentgrid.jdcloud-api.com
  data_endpoint: https://agentgrid-api-cn-north-1.jdcloud.com
  timeout: 30
  connect_timeout: 5
  read_timeout: 30
  max_retries: 2
  user_agent_extra: demo-app/1.0

例如入口脚本位于 ./examples/run.py,则配置文件应放在 ./examples/agentgrid.yaml。

最小示例(Quick Start)

显式 auth(兼容旧用法):

from agentgrid import AgentGridProfile, runtime_session

profile = AgentGridProfile(
    region="cn-north-1",
    data_endpoint="https://agentgrid-api-cn-north-1.jdcloud.com",  # 可选:默认就是该地址
)

with runtime_session("cn-north-1", "your-runtime-id", profile=profile) as runtime:
    resp = runtime.invoke(
        {"text": "你好,AgentGrid"},
        api_key="your-runtime-api-key",
    )

print(resp.get("output_text"))
print(resp.get("request_id"), resp.get("trace_id"))

默认 AK/SK 凭证链(无需显式 auth):

from agentgrid import AgentGridProfile

profile = AgentGridProfile(region="cn-north-1")

当未显式传入 auth 时,AK/SK 优先级固定为:

  1. access_key/secret_key 构造参数
  2. JDCLOUD_ACCESS_KEY/JDCLOUD_SECRET_KEY
  3. ~/.jdcloud/credentials 的 [default](access_key_id/secret_access_key)

任一来源出现半配置(仅 AK 或仅 SK)会立即报错并终止降级。

支持的认证方式(v1)

  • ApiKeyAuthProvider
  • JdcloudAkskAuthProvider

v1 仅支持以上两种认证方式。

Runtime 与会话示例

统一调用(自动 JSON/SSE 分流):

with runtime_session("cn-north-1", "rt-template-001", profile=profile) as runtime:
    result = runtime.invoke(
        {"text": "请总结这段文本"},
        api_key="your-runtime-api-key",
        metadata={"scene": "demo"},
    )

if isinstance(result, dict):
    print(result.get("output_text"))
else:
    for event in result:
        if event.type == "text_delta" and event.text:
            print(event.text, end="", flush=True)

使用 runtime_session():

from agentgrid import runtime_session

with runtime_session("cn-north-1", "chat-001", profile=profile) as runtime:
    resp = runtime.invoke("会话内调用", api_key="your-runtime-api-key")
    print(resp.get("session_id"))

# 退出上下文时仅记录 end_session 日志,不执行显式网络清理

类型模型(v1)

  • runtime.invoke() 按响应 Content-Type 自动返回:
    • JSON:dict
    • SSE:可同步迭代 StreamResponse
  • StreamEvent 统一字段:type、text、data、raw

异常与排障

统一异常基类:AgentGridError。

主要子类:

  • AgentGridAuthenticationError
  • AgentGridAuthorizationError
  • AgentGridRateLimitError
  • AgentGridTimeoutError
  • AgentGridTransportError
  • AgentGridStreamError
  • AgentGridServerError

异常对象稳定暴露诊断字段:code、request_id、retryable。

控制口与数据口边界(务必注意):

  • 控制口接口(create/update/delete/get/list)仅支持 AK/SK,使用 API Key 会抛出 AgentGridAuthenticationError(code="AUTHENTICATION_REQUIRED")
  • 数据口接口(invoke)本期强制 API Key;优先级:请求级 api_key > agentgrid.yaml 的 runtime.api_key

Metadata

Release files for jdcloud-agentgrid 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jdcloud-agentgrid 0.2.3
File Size Uploaded Provenance
jdcloud_agentgrid-0.2.3.tar.gz 36.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for jdcloud-agentgrid 0.2.3
File Interpreter ABI Platform
jdcloud_agentgrid-0.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 87.1 kB

Release files / jdcloud_agentgrid-0.2.3.tar.gz

Download URL jdcloud_agentgrid-0.2.3.tar.gz
Size 36.3 kB
Tags Source
SHA-256 checksum
How to use checksums
c762ff913aa60e975e9e0f7b15a023acbd624ed586094175cdf39e1f519aca32
BLAKE2b-256 checksum
How to use checksums
fac08eb5693bbd40678ab4a85458d9cfb749cd9316621e1b6c992459562d49b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release files / jdcloud_agentgrid-0.2.3-py3-none-any.whl

Download URL jdcloud_agentgrid-0.2.3-py3-none-any.whl
Size 50.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
59a4e95ec2b678ec267dee066810749c21e01e271ec45a02109a44d31c12cf71
BLAKE2b-256 checksum
How to use checksums
88e17b57b548c7ef3d8cdfd8e4dbc9f81578e67afb1fc70783a8054d793ab238
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Security for jdcloud-agentgrid 0.2.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page