- 1.122.0 (latest)
- 1.121.0
- 1.120.0
- 1.119.0
- 1.118.0
- 1.117.0
- 1.95.1
- 1.94.0
- 1.93.1
- 1.92.0
- 1.91.0
- 1.90.0
- 1.89.0
- 1.88.0
- 1.87.0
- 1.86.0
- 1.85.0
- 1.84.0
- 1.83.0
- 1.82.0
- 1.81.0
- 1.80.0
- 1.79.0
- 1.78.0
- 1.77.0
- 1.76.0
- 1.75.0
- 1.74.0
- 1.73.0
- 1.72.0
- 1.71.1
- 1.70.0
- 1.69.0
- 1.68.0
- 1.67.1
- 1.66.0
- 1.65.0
- 1.63.0
- 1.62.0
- 1.60.0
- 1.59.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) -> strAssembles 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():
- 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.
- 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
Partobjects are extracted and inserted into the assembled sequence. - Text segments from the template and simple text variable values
become
Part(text=...).
- Detects if any variable's value is a JSON string representing
multimodal content (specifically,
- 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 singlegoogle.genai.types.Contentobject withrole="user"and the assembled parts.
- If ALL substituted variables were simple text AND the assembled
result (after merging adjacent text parts) consists of a single,
purely textual
- Text Part Merging: Consecutively assembled text parts are
automatically merged into a single text
Partto 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) -> strAPI documentation for text_must_not_be_empty method.