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

@google/genai SDK中ai.files.upload忽略abortSignal无异常问题咨询

问题:@google/genai SDK文件上传时AbortController失效

我在Vue.js应用中使用官方@google/genai SDK,尝试通过AbortController实现文件上传的取消机制。但触发.abort()时,ai.files.upload方法完全忽略该信号——既不抛出异常、流程跳过catch块,上传仍成功完成,且abortController.signal.aborted的值为true。

最小复现代码

<script setup>
import { ref } from 'vue';
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({ apiKey: "BLABLABLABLABLA" });

const result = ref("");
let abortController;

async function sendRequest() {
  abortController = new AbortController();

  try {
    console.log("Uploading blob");
    const mockAudioBlob = new Blob(["fake audio bytes"], { type: "audio/mp3" });
    const myFile = await ai.files.upload({
      file: mockAudioBlob,
      config: {
        abortSignal: abortController.signal
      }
    });

    result.value = myFile.uri;
    console.log(myFile.uri);
    console.log("Aborted? " + abortController.signal.aborted);
  }
  catch (e) {
    if (e && e.name === "AbortError") {
      console.log("Request was aborted");
    }
    else {
      console.log(e);
    }
  }
  finally {
    abortController = null;
  }
}

function abortRequest() {
  if (abortController && abortController.signal && !abortController.signal.aborted) {
    abortController.abort();
  }
}

</script>

<template>
  <button @click="sendRequest">Upload</button>
  <button @click="abortRequest">Abort</button>
  <p>{{ result }}</p>
</template>

我预期触发abort时会抛出AbortError异常并进入catch块,请问这是SDK的预期行为还是Bug?


回答
  • 这大概率是@google/genai SDK的Bug,因为按照标准AbortController的使用逻辑,当信号触发abort后,依赖该信号的异步操作应当立即终止并抛出AbortError。
  • 参数传递无问题:你在config对象中传入abortSignal的写法符合SDK文档要求,不存在参数格式错误。
  • 临时替代方案:可以手动添加取消标记,在上传完成后判断状态并主动抛出异常,中断后续流程:
// 修改sendRequest和abortRequest逻辑
async function sendRequest() {
  abortController = new AbortController();
  let isAborted = false; // 新增取消标记

  try {
    console.log("Uploading blob");
    const mockAudioBlob = new Blob(["fake audio bytes"], { type: "audio/mp3" });
    const myFile = await ai.files.upload({
      file: mockAudioBlob,
      config: {
        abortSignal: abortController.signal
      }
    });

    // 上传完成后检查是否已取消
    if (isAborted) {
      throw new Error('AbortError');
    }

    result.value = myFile.uri;
    console.log(myFile.uri);
    console.log("Aborted? " + abortController.signal.aborted);
  }
  catch (e) {
    if (e && e.name === "AbortError") {
      console.log("Request was aborted");
    }
    else {
      console.log(e);
    }
  }
  finally {
    abortController = null;
  }
}

function abortRequest() {
  if (abortController && abortController.signal && !abortController.signal.aborted) {
    abortController.abort();
    isAborted = true; // 标记已取消
  }
}
  • 建议向Google GenAI官方代码仓库提交Issue,附上你的最小复现代码,明确说明abortSignal未被正确处理的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.01 13:27:27