Module: google.generativeai.protos

This module provides low level access to the ProtoBuffer "Message" classes used by the API.

For typical usage of this SDK you do not need to use any of these classes.

ProtoBufers are Google API's serilization format. They are strongly typed and efficient.

The genai SDK tries to be permissive about what objects it will accept from a user, but in the end the SDK always converts input to an appropriate Proto Message object to send as the request. Each API request has a *Request and *Response Message defined here.

If you have any uncertainty about what the API may accept or return, these classes provide the complete/unambiguous answer. They come from the google-ai-generativelanguage package which is generated from a snapshot of the API definition.

from google.generativeai import protos
import inspect

Proto classes can have "oneof" fields. Use in to check which oneof field is set.

p = protos.Part(text='hello')
'text' in p
p.inline_data = {'mime_type':'image/png', 'data': b'PNG'}
type(p.inline_data) is protos.Blob
'inline_data' in p
'text' in p

Instances of all Message classes can be converted into JSON compatible dictionaries with the following construct (Bytes are base64 encoded):

p_dict = type(p).to_dict(p)
{'inline_data': {'mime_type': 'image/png', 'data': 'UE5H'} }

A compatible dict can be converted to an instance of a Message class by passing it as the first argument to the constructor:

p = protos.Part(p_dict)
inline_data {
  mime_type: "image/png"
  data: "PNG"

Note when converting that to_dict accepts additional arguments:

  • use_integers_for_enums:bool = True, Set it to False to replace enum int values with their string names in the output
  • including_default_value_fields:bool = True, Set it to False to reduce the verbosity of the output.

Additional arguments are described in the docstring:



class AttributionSourceId: Identifier for the source contributing to this attribution.

class BatchCreateChunksRequest: Request to batch create Chunk\ s.

class BatchCreateChunksResponse: Response from BatchCreateChunks containing a list of created Chunk\ s.

class BatchDeleteChunksRequest: Request to batch delete Chunk\ s.

class BatchEmbedContentsRequest: Batch request to get embeddings from the model for a list of prompts.

class BatchEmbedContentsResponse: The response to a BatchEmbedContentsRequest.

class BatchEmbedTextRequest: Batch request to get a text embedding from the model.

class BatchEmbedTextResponse: The response to a EmbedTextRequest.

class BatchUpdateChunksRequest: Request to batch update Chunk\ s.

class BatchUpdateChunksResponse: Response from BatchUpdateChunks containing a list of updated Chunk\ s.

class Blob: Raw media bytes.

class Candidate: A response candidate generated from the model.

class Chunk: A Chunk is a subpart of a Document that is treated as an independent unit for the purposes of vector representation and storage.

class ChunkData: Extracted data that represents the Chunk content.

class CitationMetadata: A collection of source attributions for a piece of content.

class CitationSource: A citation to a source for a portion of a specific response.

class Condition: Filter condition applicable to a single key.

class Content: The base structured datatype containing multi-part content of a message.

class ContentEmbedding: A list of floats representing an embedding.

class ContentFilter: Content filtering metadata associated with processing a single request.

class Corpus: A Corpus is a collection of Document\ s.

class CountMessageTokensRequest: Counts the number of tokens in the prompt sent to a model.

class CountMessageTokensResponse: A response from CountMessageTokens.

class CountTextTokensRequest: Counts the number of tokens in the prompt sent to a model.

class CountTextTokensResponse: A response from CountTextTokens.

class CountTokensRequest: Counts the number of tokens in the prompt sent to a model.

class CountTokensResponse: A response from CountTokens.

class CreateChunkRequest: Request to create a Chunk.

class CreateCorpusRequest: Request to create a Corpus.

class CreateDocumentRequest: Request to create a Document.

class CreateFileRequest: Request for CreateFile.

class CreateFileResponse: Response for CreateFile.

class CreatePermissionRequest: Request to create a Permission.

class CreateTunedModelMetadata: Metadata about the state and progress of creating a tuned model returned from the long-running operation

class CreateTunedModelRequest: Request to create a TunedModel.

class CustomMetadata: User provided metadata stored as key-value pairs.

class Dataset: Dataset for training or validation.

class DeleteChunkRequest: Request to delete a Chunk.

class DeleteCorpusRequest: Request to delete a Corpus.

class DeleteDocumentRequest: Request to delete a Document.

class DeleteFileRequest: Request for DeleteFile.

class DeletePermissionRequest: Request to delete the Permission.

class DeleteTunedModelRequest: Request to delete a TunedModel.

class Document: A Document is a collection of Chunk\ s.

class EmbedContentRequest: Request containing the Content for the model to embed.

class EmbedContentResponse: The response to an EmbedContentRequest.

class EmbedTextRequest: Request to get a text embedding from the model.

class EmbedTextResponse: The response to a EmbedTextRequest.

class Embedding: A list of floats representing the embedding.

class Example: An input/output example used to instruct the Model.

class File: A file uploaded to the API.

class FileData: URI based data.

class FunctionCall: A predicted FunctionCall returned from the model that contains a string representing the with the arguments and their values.

class FunctionCallingConfig: Configuration for specifying function calling behavior.

class FunctionDeclaration: Structured representation of a function declaration as defined by the OpenAPI 3.03 specification <<a href=""></a>>__.

class FunctionResponse: The result output from a FunctionCall that contains a string representing the and a structured JSON object containing any output from the function is used as context to the model.

class GenerateAnswerRequest: Request to generate a grounded answer from the model.

class GenerateAnswerResponse: Response from the model for a grounded answer.

class GenerateContentRequest: Request to generate a completion from the model.

class GenerateContentResponse: Response from the model supporting multiple candidates.

class GenerateMessageRequest: Request to generate a message response from the model.

class GenerateMessageResponse: The response from the model.

class GenerateTextRequest: Request to generate a text completion response from the model.

class GenerateTextResponse: The response from the model, including candidate completions.

class GenerationConfig: Configuration options for model generation and outputs.

class GetChunkRequest: Request for getting information about a specific Chunk.

class GetCorpusRequest: Request for getting information about a specific Corpus.

class GetDocumentRequest: Request for getting information about a specific Document.

class GetFileRequest: Request for GetFile.

class GetModelRequest: Request for getting information about a specific Model.

class GetPermissionRequest: Request for getting information about a specific Permission.

class GetTunedModelRequest: Request for getting information about a specific Model.

class GroundingAttribution: Attribution for a source that contributed to an answer.

class GroundingPassage: Passage included inline with a grounding configuration.

class GroundingPassages: A repeated list of passages.

class HarmCategory: The category of a rating.

class Hyperparameters: Hyperparameters controlling the tuning process.

class ListChunksRequest: Request for listing Chunk\ s.

class ListChunksResponse: Response from ListChunks containing a paginated list of Chunk\ s.

class ListCorporaRequest: Request for listing Corpora.

class ListCorporaResponse: Response from ListCorpora containing a paginated list of Corpora.

class ListDocumentsRequest: Request for listing Document\ s.

class ListDocumentsResponse: Response from ListDocuments containing a paginated list of Document\ s.

class ListFilesRequest: Request for ListFiles.

class ListFilesResponse: Response for ListFiles.

class ListModelsRequest: Request for listing all Models.

class ListModelsResponse: Response from ListModel containing a paginated list of Models.

class ListPermissionsRequest: Request for listing permissions.

class ListPermissionsResponse: Response from ListPermissions containing a paginated list of permissions.

class ListTunedModelsRequest: Request for listing TunedModels.

class ListTunedModelsResponse: Response from ListTunedModels containing a paginated list of Models.

class Message: The base unit of structured text.

class MessagePrompt: All of the structured input text passed to the model as a prompt.

class MetadataFilter: User provided filter to limit retrieval based on Chunk or Document level metadata values.

class Model: Information about a Generative Language Model.

class Part: A datatype containing media that is part of a multi-part Content message.

class Permission: Permission resource grants user, group or the rest of the world access to the PaLM API resource (e.g.

class QueryCorpusRequest: Request for querying a Corpus.

class QueryCorpusResponse: Response from QueryCorpus containing a list of relevant chunks.

class QueryDocumentRequest: Request for querying a Document.

class QueryDocumentResponse: Response from QueryDocument containing a list of relevant chunks.

class RelevantChunk: The information for a chunk relevant to a query.

class SafetyFeedback: Safety feedback for an entire request.

class SafetyRating: Safety rating for a piece of content.

class SafetySetting: Safety setting, affecting the safety-blocking behavior.

class Schema: The Schema object allows the definition of input and output data types.

class SemanticRetrieverConfig: Configuration for retrieving grounding content from a Corpus or Document created using the Semantic Retriever API.

class StringList: User provided string values assigned to a single metadata key.

class TaskType: Type of task for which the embedding will be used.

class TextCompletion: Output text returned from a model.

class TextPrompt: Text given to the model as a prompt.

class Tool: Tool details that the model may use to generate response.

class ToolConfig: The Tool configuration containing parameters for specifying Tool use in the request.

class TransferOwnershipRequest: Request to transfer the ownership of the tuned model.

class TransferOwnershipResponse: Response from TransferOwnership.

class TunedModel: A fine-tuned model created using ModelService.CreateTunedModel.

class TunedModelSource: Tuned model as a source for training a new model.

class TuningExample: A single example for tuning.

class TuningExamples: A set of tuning examples. Can be training or validation data.

class TuningSnapshot: Record for a single tuning step.

class TuningTask: Tuning tasks that create tuned models.

class Type: Type contains the list of OpenAPI data types as defined by

class UpdateChunkRequest: Request to update a Chunk.

class UpdateCorpusRequest: Request to update a Corpus.

class UpdateDocumentRequest: Request to update a Document.

class UpdatePermissionRequest: Request to update the Permission.

class UpdateTunedModelRequest: Request to update a TunedModel.

class VideoMetadata: Metadata for a video File.