You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法通过Python启动Vertex AI自定义任务,请求技术协助

问题分析与解决方案

核心问题:API端点配置错误

你遇到的hpack_parser解析错误和Unknown: None Stream removed异常,最可能的原因是API端点地址拼写错误。

在你的Python代码中,传入的api_endpoint参数是"eu-west1-aiplatform.googleapis.com",但Google Cloud Vertex AI欧洲西部1(europe-west1)区域的正确API端点应该是"europe-west1-aiplatform.googleapis.com"(前缀是europe-west1而非eu-west1)。

gcloud命令能正常运行是因为你指定了正确的--region=europe-west1,gcloud会自动映射到正确的端点;而Python代码中手动指定了错误的端点,导致请求被发送到不存在的地址,返回了HTML错误页面(因此出现content-type=text/html的解析错误)。

修正后的代码

将api_endpoint参数修正为正确的地址,并确保容器镜像URI中的占位符已替换为实际值:

from google.cloud import aiplatform


def create_custom_job_sample(
    project: str,
    display_name: str,
    container_image_uri: str,
    location: str = "us-central1",
    api_endpoint: str = "us-central1-aiplatform.googleapis.com",
):
    # The AI Platform services require regional API endpoints.
    client_options = {"api_endpoint": api_endpoint}
    # Initialize client that will be used to create and send requests.
    # This client only needs to be created once, and can be reused for multiple requests.
    client = aiplatform.gapic.JobServiceClient(client_options=client_options)
    custom_job = {
        "display_name": display_name,
        "job_spec": {
            "worker_pool_specs": [
                {
                    "machine_spec": {
                        "machine_type": "n1-standard-4"
                    },
                    "replica_count": 1,
                    "container_spec": {
                        "image_uri": container_image_uri,
                        "command": [],
                        "args": [],
                    },
                }
            ]
        },
    }
    parent = f"projects/{project}/locations/{location}"
    response = client.create_custom_job(parent=parent, custom_job=custom_job)
    print("response:", response)

create_custom_job_sample(
    "MY_PROJECT",
    "job-123",
    "europe-west1-docker.pkg.dev/你的项目ID/你的镜像路径",
    "europe-west1",
    "europe-west1-aiplatform.googleapis.com"  # 此处修正为正确端点
)

额外验证步骤

  • 确认container_image_uri中的占位符已替换为实际的Google Cloud项目ID和镜像完整路径(与gcloud命令中使用的一致)。
  • 确保你的Python环境已正确配置Google Cloud认证(可通过gcloud auth application-default login完成本地认证)。
  • 检查项目是否已启用Vertex AI API(gcloud能运行说明已启用,也可在Google Cloud控制台确认)。

内容的提问来源于stack exchange,提问作者Galuoises

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 11:24:11