Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Googleapis Python genai Part From Text

From Leeroopedia
Knowledge Sources
Domains NLP, Data_Preparation
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete tool for constructing text content parts and structured messages provided by the google-genai types module.

Description

Part.from_text is a class method that creates a Part containing text data. The Content class groups multiple Parts into a single message with an optional role (user or model). Together, these types form the structured input expected by the generation API. The SDK also accepts raw strings and lists as shorthand, automatically transforming them into proper Content objects via internal transformer functions.

Usage

Use Part.from_text when you need explicit control over content construction, such as combining text with other part types (images, function responses) in a single message. For simple text-only input, passing a raw string directly to generate_content is sufficient as the SDK handles the conversion automatically.

Code Reference

Source Location

  • Repository: googleapis/python-genai
  • File: google/genai/types.py
  • Lines: L1761-1762 (Part.from_text), L1860 (Content class)

Signature

class Part(_common.BaseModel):
    @classmethod
    def from_text(cls, *, text: str) -> 'Part':
        """Creates a Part containing text data.

        Args:
            text: The text content.
        """

class Content(_common.BaseModel):
    """Contains the multi-part content of a message."""
    parts: Optional[list[Part]] = None
    role: Optional[str] = None  # 'user' or 'model'

Import

from google.genai import types

I/O Contract

Inputs

Name Type Required Description
text str Yes Text content to wrap in a Part (for Part.from_text)
parts Optional[list[Part]] No List of parts for Content object
role Optional[str] No Role: 'user' or 'model'

Outputs

Name Type Description
Part.from_text returns Part A Part object containing text data
Content instance Content A message containing parts with optional role

Usage Examples

Simple Text Part

from google.genai import types

# Create a text part
part = types.Part.from_text(text="What is the capital of France?")

# Wrap in Content with role
content = types.Content(parts=[part], role="user")

Direct String (SDK Auto-Conversion)

from google import genai

client = genai.Client(api_key="YOUR_API_KEY")

# The SDK automatically converts strings to Content objects
response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents="What is the capital of France?"
)

Multi-Turn Content List

from google.genai import types

contents = [
    types.Content(
        parts=[types.Part.from_text(text="What is Python?")],
        role="user"
    ),
    types.Content(
        parts=[types.Part.from_text(text="Python is a programming language.")],
        role="model"
    ),
    types.Content(
        parts=[types.Part.from_text(text="What are its main features?")],
        role="user"
    ),
]

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment