Class PromptTemplate (1.129.0)

PromptTemplate(*, text: typing.Optional[str] = None)

A prompt template for creating prompts with variables.

Properties

variables

API documentation for variables property.

Methods

PromptTemplate

PromptTemplate(*, text: typing.Optional[str] = None)

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

assemble

assemble(**kwargs: typing.Any) -> str

Assembles the prompt template with the given keyword arguments.

Supports both text and multimodal content. The assemble method substitutes variables from the prompt template text with provided values.

Key Behaviors of assemble():

  1. Variable Substitution: Replaces all defined variables with their corresponding keyword argument values. Raises ValueError if a template variable is missing a value or if an extraneous kwarg is provided.
  2. Multimodal Handling:
    • Detects if any variable's value is a JSON string representing multimodal content (specifically, {"contents": [{"parts": [...]}]} or {"role": "user", "parts": [...]}).
    • If multimodal content is detected for a variable, its Part objects are extracted and inserted into the assembled sequence.
    • Text segments from the template and simple text variable values become Part(text=...).
  3. Output Format:
    • If ALL substituted variables were simple text AND the assembled result (after merging adjacent text parts) consists of a single, purely textual Part, assemble() returns a raw Python string.
    • Otherwise (if any variable was multimodal, or if the assembly results in multiple parts or non-textual parts), assemble() returns a JSON string representing a single google.genai.types.Content object with role="user" and the assembled parts.
  4. Text Part Merging: Consecutively assembled text parts are automatically merged into a single text Part to create a more concise list of parts.

This dual output format (raw string or JSON string of Content) allows the downstream inference functions to seamlessly handle both simple text prompts and more complex multimodal prompts generated from the same templating mechanism.

text_must_not_be_empty

text_must_not_be_empty(value: str) -> str

API documentation for text_must_not_be_empty method.