@@ -1602,15 +1602,8 @@ type Model struct {
16021602 OwnedBy string `json:"owned_by"`
16031603}
16041604
1605- // EmbeddingRequest represents a request structure for embeddings API.
1606- type EmbeddingRequest struct {
1607- // Input: Input text to embed, encoded as a string or array of tokens.
1608- // To embed multiple inputs in a single request, pass an array of strings or array of token arrays.
1609- // The input must not exceed the max input tokens for the model (8192 tokens for text-embedding-ada-002),
1610- // cannot be an empty string, and any array must be 2048 dimensions or less.
1611- // Docs: https://platform.openai.com/docs/api-reference/embeddings/create#embeddings-create-input
1612- Input EmbeddingRequestInput `json:"input"`
1613-
1605+ // EmbeddingBaseRequest holds fields shared by both embedding request variants.
1606+ type EmbeddingBaseRequest struct {
16141607 // Model: ID of the model to use.
16151608 // Docs: https://platform.openai.com/docs/api-reference/embeddings/create#embeddings-create-model
16161609 Model string `json:"model"`
@@ -1632,6 +1625,76 @@ type EmbeddingRequest struct {
16321625 * GCPVertexAIEmbeddingVendorFields `json:",inline,omitempty"`
16331626}
16341627
1628+ // EmbeddingCompletionRequest is the text-only embedding request (classic OpenAI style).
1629+ // https://developers.openai.com/api/reference/resources/embeddings/methods/create
1630+ type EmbeddingCompletionRequest struct {
1631+ EmbeddingBaseRequest
1632+ // Input: Input text to embed, encoded as a string or array of tokens.
1633+ // To embed multiple inputs in a single request, pass an array of strings or array of token arrays.
1634+ // The input must not exceed the max input tokens for the model (8192 tokens for text-embedding-ada-002),
1635+ // cannot be an empty string, and any array must be 2048 dimensions or less.
1636+ Input EmbeddingRequestInput `json:"input"`
1637+ }
1638+
1639+ // EmbeddingChatRequest is the chat-style embedding request (supports multimodal).
1640+ // Following vLLM convention: https://github.com/vllm-project/vllm/blob/2a16ece2d342c0c154a4949ad317b521f8c04ec4/vllm/entrypoints/pooling/embed/protocol.py#L83
1641+ type EmbeddingChatRequest struct {
1642+ EmbeddingBaseRequest
1643+ // Messages: Chat messages for multimodal embedding.
1644+ // Uses the same chat message format as /v1/chat/completions.
1645+ // Only user messages are processed; system/assistant/tool messages are ignored.
1646+ Messages []ChatCompletionMessageParamUnion `json:"messages"`
1647+ }
1648+
1649+ // EmbeddingRequest is a discriminated union: exactly one of OfCompletion or OfChat is set.
1650+ // Discrimination is by presence of "input" (→ completion) vs "messages" (→ chat) in JSON.
1651+ type EmbeddingRequest struct {
1652+ EmbeddingBaseRequest // promoted shared fields
1653+ OfCompletion * EmbeddingCompletionRequest // set when JSON has "input"
1654+ OfChat * EmbeddingChatRequest // set when JSON has "messages"
1655+ }
1656+
1657+ // UnmarshalJSON discriminates between completion and chat embedding requests.
1658+ func (r * EmbeddingRequest ) UnmarshalJSON (data []byte ) error {
1659+ hasInput := gjson .GetBytes (data , "input" ).Exists ()
1660+ hasMessages := gjson .GetBytes (data , "messages" ).Exists ()
1661+
1662+ if hasInput && hasMessages {
1663+ return fmt .Errorf ("embedding request must have either 'input' or 'messages', not both" )
1664+ }
1665+
1666+ if hasMessages {
1667+ var chat EmbeddingChatRequest
1668+ if err := json .Unmarshal (data , & chat ); err != nil {
1669+ return err
1670+ }
1671+ r .EmbeddingBaseRequest = chat .EmbeddingBaseRequest
1672+ r .OfChat = & chat
1673+ return nil
1674+ }
1675+
1676+ // Default to completion (input-based) — this handles both explicit "input" and legacy cases.
1677+ var comp EmbeddingCompletionRequest
1678+ if err := json .Unmarshal (data , & comp ); err != nil {
1679+ return err
1680+ }
1681+ r .EmbeddingBaseRequest = comp .EmbeddingBaseRequest
1682+ r .OfCompletion = & comp
1683+ return nil
1684+ }
1685+
1686+ // MarshalJSON delegates to the active variant.
1687+ func (r EmbeddingRequest ) MarshalJSON () ([]byte , error ) {
1688+ if r .OfChat != nil {
1689+ return json .Marshal (r .OfChat )
1690+ }
1691+ if r .OfCompletion != nil {
1692+ return json .Marshal (r .OfCompletion )
1693+ }
1694+ // Fallback: marshal just the base fields.
1695+ return json .Marshal (r .EmbeddingBaseRequest )
1696+ }
1697+
16351698type EmbeddingTaskType string
16361699
16371700const (
@@ -1645,16 +1708,20 @@ const (
16451708 EmbeddingTaskTypeCodeRetrievalQuery EmbeddingTaskType = "CODE_RETRIEVAL_QUERY"
16461709)
16471710
1648- // GCPVertexAIEmbeddingVendorFields contains GCP Vertex AI (Gemini) vendor-specific fields for embeddings.
1711+ // GCPVertexAIEmbeddingVendorFields contains GCP Vertex AI vendor-specific fields for embeddings.
1712+ // The translator maps these to the appropriate wire format per endpoint:
1713+ // - predict: parameters.auto_truncate, instances[].task_type, instances[].title
1714+ // - embedContent: embedContentConfig.autoTruncate, embedContentConfig.taskType, embedContentConfig.title
16491715type GCPVertexAIEmbeddingVendorFields struct {
1650- // When set to true, input text will be truncated. When set to false, an error is returned if the input text is longer than the maximum length supported by the model. Defaults to true.
1651- // https://docs.cloud.google.com/vertex-ai/generative-ai/docs/model-reference/text-embeddings-api#parameter-list
1652-
1716+ // Auto-truncate input text if it exceeds the model's max length. Defaults to true.
16531717 AutoTruncate bool `json:"auto_truncate,omitempty"`
16541718
1655- // This is global task_type set, which is convenient for users. If left blank, the default used is RETRIEVAL_QUERY.
1656- // For more information about task types, see https://docs.cloud.google.com/vertex-ai/generative-ai/docs/embeddings/task-types
1719+ // Global task type for the request. Defaults to RETRIEVAL_QUERY if unset .
1720+ // https://docs.cloud.google.com/vertex-ai/generative-ai/docs/embeddings/task-types
16571721 TaskType EmbeddingTaskType `json:"task_type,omitempty"`
1722+
1723+ // Title for the embedding content. Helps the model produce better embeddings.
1724+ Title string `json:"title,omitempty"`
16581725}
16591726
16601727// EmbeddingResponse represents a response from /v1/embeddings.
0 commit comments