From 5e326b35d1842a19416e3c1148c64dc362910c11 Mon Sep 17 00:00:00 2001 From: "Owen W. Taylor" Date: Thu, 19 Nov 2026 10:23:03 -0500 Subject: [PATCH] RSPEED-3958 Fix Vertex AI failures with parallel tool calls Multiple tool calls returned from the Vertex api in different chunks would be squashed together resulting in invalid JSON. Add a top-level patch/ directory for fixes to bundled Python packages that can't wait for an upstream release: patches in patch//NNN-*.patch are applied in order to the installed package in the builder stage of the Containerfile. Add patch/ogx/001-vertexai-number-streamed-tool-calls.patch, a backport of https://github.com/ogx-ai/ogx/pull/6775 to ogx 1.2. (Equivalent of #3186 for release/0.6.) Co-Authored-By: Claude Opus 5.5 --- deploy/lightspeed-stack/Containerfile | 17 ++++++++- patch/README.md | 17 +++++++++ ...-vertexai-number-streamed-tool-calls.patch | 35 +++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 patch/README.md create mode 100644 patch/ogx/001-vertexai-number-streamed-tool-calls.patch diff --git a/deploy/lightspeed-stack/Containerfile b/deploy/lightspeed-stack/Containerfile index bfd70e178..8fb2bb7ea 100644 --- a/deploy/lightspeed-stack/Containerfile +++ b/deploy/lightspeed-stack/Containerfile @@ -24,7 +24,7 @@ USER root # Install gcc - required by polyleven python package on aarch64 # (dependency of autoevals, no pre-built binary wheels for linux on aarch64) # cmake and cargo are required by fastuuid, maturin -RUN ${BUILDER_DNF_COMMAND} install -y --nodocs --setopt=keepcache=0 --setopt=tsflags=nodocs gcc gcc-c++ cmake cargo && \ +RUN ${BUILDER_DNF_COMMAND} install -y --nodocs --setopt=keepcache=0 --setopt=tsflags=nodocs gcc gcc-c++ cmake cargo patch && \ ${BUILDER_DNF_COMMAND} update -y --nodocs --setopt=keepcache=0 --setopt=tsflags=nodocs --allowerasing # Install uv package manager @@ -54,6 +54,21 @@ RUN if [ -f /cachi2/cachi2.env ]; then \ uv sync --locked --no-dev --group ogxlibdev; \ fi +# Apply patches to bundled Python packages (see patch/README.md). +# patch//NNN-*.patch files are applied in order to +# site-packages/. +COPY ${LSC_SOURCE_DIR}/patch /tmp/patch +RUN set -eu; \ + SITE_PACKAGES=/app-root/.venv/lib/python3.12/site-packages; \ + for pkg_dir in /tmp/patch/*/; do \ + pkg="$(basename "${pkg_dir}")"; \ + for patch_file in "${pkg_dir}"[0-9][0-9][0-9]-*.patch; do \ + echo "Applying ${patch_file}"; \ + patch -p1 --batch --forward --fuzz=0 -d "${SITE_PACKAGES}/${pkg}" < "${patch_file}"; \ + done; \ + done; \ + rm -rf /tmp/patch + # Explicitly remove some packages to mitigate some CVEs # - GHSA-wj6h-64fc-37mp: python-ecdsa package won't fix it upstream. # This package is required by python-jose. python-jose supports multiple diff --git a/patch/README.md b/patch/README.md new file mode 100644 index 000000000..c3bd5341b --- /dev/null +++ b/patch/README.md @@ -0,0 +1,17 @@ +# Patches for bundled Python packages + +Patches in this directory are applied to installed Python packages while +building the container image (see `deploy/lightspeed-stack/Containerfile`). +Use them only for urgent fixes that cannot wait for a new upstream release, +and remove them once the dependency is upgraded to a version with the fix. + +Layout: + +- One subdirectory per patched package, named after the package's import + directory in `site-packages` (for example `ogx`). +- Patch files are named `NNN-short-description.patch` and are applied in + numeric order. +- Paths in the patch are relative to the package directory and use `a/` and + `b/` prefixes; they are applied with + `patch -p1 -d site-packages/`. +- Begin each patch with a description that links to the upstream change. diff --git a/patch/ogx/001-vertexai-number-streamed-tool-calls.patch b/patch/ogx/001-vertexai-number-streamed-tool-calls.patch new file mode 100644 index 000000000..beefb7227 --- /dev/null +++ b/patch/ogx/001-vertexai-number-streamed-tool-calls.patch @@ -0,0 +1,35 @@ +Number streamed Vertex AI tool calls sequentially + +Backport of https://github.com/ogx-ai/ogx/pull/6775 +(fixes https://github.com/ogx-ai/ogx/issues/6774). + +Gemini sends each function call whole, and the tool call positions restart +at 0 in every streamed chunk. When parallel function calls arrive in +separate chunks they all get index 0, and consumers merge them into a +single call whose arguments are concatenated (invalid) JSON. Assign +indices sequentially across the stream. + +Drop this patch once ogx includes the upstream fix. + +--- a/providers/remote/inference/vertexai/vertexai.py ++++ b/providers/remote/inference/vertexai/vertexai.py +@@ -641,6 +641,7 @@ class VertexAIInferenceAdapter(NeedsRequestProviderData, BaseModel): + async def _iter() -> AsyncIterator[OpenAIChatCompletionChunk]: + is_first_chunk = True + last_chunk: Any = None ++ next_tool_call_index = 0 + async for chunk in stream: + signatures: dict[str, str] = {} + openai_chunk = converters.convert_gemini_stream_chunk_to_openai( +@@ -650,6 +651,11 @@ class VertexAIInferenceAdapter(NeedsRequestProviderData, BaseModel): + is_first_chunk=is_first_chunk, + signatures_out=signatures, + ) ++ # Gemini sends whole function calls, and positions restart in each chunk. ++ for choice in openai_chunk.choices: ++ for tool_call in choice.delta.tool_calls or []: ++ tool_call.index = next_tool_call_index ++ next_tool_call_index += 1 + # Persist before yield: code after yield only runs when the + # consumer pulls the next item, and is skipped if the stream is + # closed after this chunk (client has the call id already).