fix(genkit-tools): Refactor Tools API to prevent proxy 503 errors#4807
fix(genkit-tools): Refactor Tools API to prevent proxy 503 errors#4807MichaelDoyle wants to merge 4 commits intomainfrom
Conversation
Summary of ChangesHello @MichaelDoyle, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refactors the Genkit Tools server API endpoints to enhance reliability in environments with restrictive proxies, such as Google Cloud Shell. The core change involves shifting the early transmission of the trace ID from HTTP headers to the response body, which helps prevent 503 Service Unavailable errors caused by perceived inactivity. The update standardizes streaming responses to use NDJSON and improves error handling for both initial request processing and ongoing streams. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
The pull request successfully refactors the Genkit Tools server API endpoints (/api/runAction, /api/streamAction, /api/streamTrace) to address proxy 503 errors. This is achieved by shifting the traceId from HTTP headers to the response body, sending it as the very first line in newline-delimited JSON (NDJSON) format. This approach correctly prevents proxy timeouts by providing immediate activity (TTFB) and standardizes the streaming protocol. Error handling is also improved to ensure a 500 status code is sent if a failure occurs before the first chunk.
However, this change introduces a critical breaking change for existing clients, specifically the RuntimeManager in manager.ts. The RuntimeManager currently expects the X-Genkit-Trace-Id in HTTP headers and is not updated in this pull request to parse the response body as an NDJSON stream to extract the trace ID. This client-side update is essential for the functionality of these endpoints and needs to be addressed to ensure the system works as intended with the new server-side protocol.
|
Dev UI side: FirebasePrivate/genkit-ui/pull/1760 |
| // Send the initial trace ID, which will also flush headers and | ||
| // serve as a "keep alive" while we wait for the action to | ||
| // complete. | ||
| res.write(JSON.stringify({ telemetry: { traceId } }) + '\n'); |
There was a problem hiding this comment.
doesn't this require Transfer-Encoding: chunked to work properly?
There was a problem hiding this comment.
It's one of those things that just need work in practice... but it might be related to your other comment (buffering by the proxy)... there may not be enough signals to the proxy that it should not be buffering things.
chunked transfer encoding should be supported by a properly implemented proxy...
|
One thing I noticed, the data does not "stream" in Cloud Shell - trying to figure out a path for that, but it doesn't seem like we're introducing that problem here, so we might want to punt for the next PR. I can see all the chunks, but the proxy is buffering the data. |
|
I've been testing in Cloud Shell. Here is the current state of the world with this PR:
|

Refactored the Genkit Tools server API endpoints (
/api/runAction,/api/streamAction,/api/streamTrace) to improve reliability in proxied environments like Google Cloud Shell. Shifts the "early information" contract from HTTP headers to the response body, requiring the client to parse these endpoints as streams.Rationale
The previous implementation used manual header flushing (
res.flushHeaders()) to send theX-Genkit-Trace-Idheader early. In Google Cloud Shell, which routes traffic through a web proxy, this "headers-only" response followed by a long silence (while waiting for AI models) triggered a 503 Service Unavailable error (due to perceived inactivity?).Key Changes
X-Genkit-Trace-IdHTTP header and its CORS exposure. ThetraceIdis now sent as the very first line of the response body in JSON format.flushHeaders()andres.statusCode = 200calls. The firstres.write()of the Trace ID chunk now naturallytriggers header transmission and provides immediate activity (TTFB), preventing proxy timeouts.
catchblocks to ensure an HTTP 500 status is sent if a failure occurs before the first chunk. If a failure occurs during streaming, it is appended as a final JSON chunk.Fixes #4796