file_uploads_processor.go
Overview
This source file provides specialized handling for file upload requests within the internal LLM (Large Language Model) integration layer. Specifically, it addresses compatibility issues with the Gemini API backend by modifying file upload requests to comply with its constraints. The Gemini API backend does not support the display_name parameter on file uploads, and this file ensures that any such fields are removed to prevent request failures.
The file contains utility logic that operates on LLM request data structures defined elsewhere in the system, modifying them in-place before the requests are sent to the backend.
Detailed Explanation
Package
llminternal: This package hosts internal logic related to LLM integration and processing. It interacts with LLM request models and the agent invocation context to manipulate requests before forwarding.
Imports
agent- Provides theInvocationContextfor the current agent execution.googlellm- Contains logic to identify the Google LLM variant in use (e.g., Gemini API).model- Defines theLLMRequestand related data structures.
Function: removeDisplayNameIfExists
func removeDisplayNameIfExists(ctx agent.InvocationContext, req *model.LLMRequest) error
Purpose
Removes the display_name field from all inline and file parts within the LLM request contents if the backend variant is Gemini API.
Parameters
ctx agent.InvocationContext: Provides context for the current invocation, including metadata and lifecycle info. Although not used directly in this function, it is passed to comply with the invocation interface pattern.req *model.LLMRequest: Pointer to the LLM request object that contains the contents and file parts potentially havingdisplay_namefields.
Return Value
error: Always returnsnilas the current implementation does not produce errors.
Behavior and Implementation Details
Checks if the
Contentsfield in the request isnil. If so, it returns immediately.Iterates over each content block in the request.
For each content, checks if
Partsexists; if missing, skips to next content.Checks if the current LLM variant is Gemini API by calling
googlellm.GetGoogleLLMVariant().If the variant is Gemini API:
Iterates over each part in the content.
For parts containing
InlineData, setsInlineData.DisplayNameto an empty string.For parts containing
FileData, setsFileData.DisplayNameto an empty string.
This logic ensures that requests sent to Gemini API exclude the unsupported display_name field, preventing backend request errors.
Example Usage
var llmReq model.LLMRequest
// llmReq is populated with contents that may include display names
err := removeDisplayNameIfExists(ctx, &llmReq)
if err != nil {
// handle error (currently always nil)
}
// Proceed with sending llmReq to Gemini API backend
Important Implementation Details
The function mutates the
LLMRequestin place.It relies on the variant detection logic from the
googlellmpackage to conditionally apply modifications only for Gemini API.The function gracefully handles cases where
ContentsorPartsarenilby early returns or skips.The absence of errors returned simplifies integration with calling code.
Interaction with Other System Components
Agent Invocation Context (
agent.InvocationContext): Passed for potential contextual info but not explicitly used here.LLMRequest Model (
model.LLMRequest): The core data structure representing the request sent to LLM backends. This file modifies parts of this structure.Google LLM Variant Detection (
googlellm.GetGoogleLLMVariant()): Determines if the Gemini API is the current backend, to apply the necessary mutation.
This function is likely called during the request preparation phase in the LLM agent pipeline, possibly integrated with Agent Invocation Context and LLM Integration and Agents for preprocessing requests before dispatch.
Mermaid Diagram
flowchart TD
A[LLMRequest] --> B[Contents]
B --> C[Content.Parts]
C --> D1[Part.InlineData]
C --> D2[Part.FileData]
subgraph GeminiAPI Check
E["googlellm.GetGoogleLLMVariant()"]
end
E -- "If Gemini API" --> F[Remove DisplayName fields]
F -->|InlineData| D1
F -->|FileData| D2
style A fill:#f9f,stroke:#333,stroke-width:1px
style F fill:#bbf,stroke:#333,stroke-width:1px
This diagram illustrates the structure of the LLMRequest and the conditional logic applied by removeDisplayNameIfExists to remove DisplayName fields from inline and file data parts when the Gemini API variant is detected.