Chapter 1: Introduction to Gemma 4 and the Book Project
What You Will Learn
Identify appropriate Gemma 4 model variants for specific support automation tasks
Explain the safety and privacy constraints governing automated ticket triage systems
Instantiate the domain model entities to represent support tickets and triage results
Configure project boundaries including escalation thresholds and redaction requirements
Describe the architectural separation between the TriageService boundary and underlying model implementations
Prerequisites
No additional prerequisites beyond those listed in About This Book.
Concept Overview
Gemma 4 represents Google's latest family of open-weight large language models. The family includes E2B and E4B edge models, a 26B Mixture-of-Experts (MoE) model with 4B active parameters, and a 31B dense model. Each size offers a base variant for continued pre-training. It also offers an instruction-tuned (IT) variant for immediate task execution. Base models require custom fine-tuning to follow instructions. IT variants understand chat templates out of the box.
Unlike closed API services, these weights run entirely within your infrastructure. You retain full control over data flow, customization, and deployment topology. This autonomy is essential when processing sensitive customer support data where third-party transmission creates compliance risk.
The book builds a single continuous project: a support ticket triage assistant for a SaaS product. This system ingests inbound customer tickets, classifies issue types, assigns priority levels, generates summaries, extracts structured account context, drafts replies, and flags escalations. The assistant operates as an offline batch pipeline for most chapters. It evolves into a deployed FastAPI service in the final sections.
Open-weight workflows demand explicit safety architecture. You cannot rely on vendor-side content filters or human review queues. Safety controls must live in four places: the application layer (input validation and sanitization), prompt assets (system prompts that forbid unsafe outputs), post-processing (output validation and redaction), and human workflow (approval queues for escalations and replies). Gemma 4 provides the reasoning capability; your code provides the guardrails.
Gemma 4 is natively multimodal, supporting text and image inputs across all sizes (with audio support on edge models). This book focuses exclusively on the text modality, which covers the support ticket triage use case entirely. The multimodal capabilities remain available if you later extend the assistant to process screenshot attachments or other visual inputs.
Base models suit continued pre-training or custom instruction tuning on proprietary support corpora. Instruction-tuned variants offer immediate task execution with proper prompt formatting. Parameter sizes range from lightweight variants suitable for CPU inference to large models requiring substantial GPU memory.
The E4B IT variant fits comfortably on consumer GPUs with 12GB VRAM. It also runs on CPU for low-volume batch jobs. The 26B MoE model activates only 4B parameters per forward pass, delivering strong accuracy on approximately 24GB VRAM while keeping inference fast. The 31B dense model demands 72GB+ VRAM or multi-GPU setups. Reserve this for high-accuracy batch processing where latency matters less than precision. Your hardware constraints determine the viable subset.
The TriageService boundary encapsulates all Gemma 4 specifics: model loading, tokenization, chat template application, generation parameters, and response parsing. It does not own business rules, persistence, or final escalation decisions. Keep the service behind an interface. This lets you swap model variants without touching workflow code.
Hardcoded safety rules are a liability. Version them or accept silent drift.
Version every asset that influences behavior. Store prompt templates, label schemas, and safety rules as versioned files in your repository. Load these at runtime. Operations teams can then adjust thresholds without code changes.
The domain imposes strict constraints. Customer support data contains names, emails, account numbers, and financial details. Training or inference on unredacted data violates privacy regulations. Escalation triggers, including refund demands, security breach reports, legal threats, and production outages, require human oversight. The system must never autonomously send replies or escalate tickets without approval.
I once debugged a production outage caused by a model hallucinating 'billing_dispute' instead of 'billing'. An enum would have caught this immediately at the API boundary.
Step-by-Step Implementation
Step 1: Define Classification Schemas
Type-safe enums prevent invalid labels from propagating through the pipeline. Define the core taxonomies for issue classification, priority assignment, escalation routing, and product area identification.
from enum import Enum
class IssueType(Enum):
BILLING = "billing"
LOGIN = "login"
BUG_REPORT = "bug_report"
FEATURE_REQUEST = "feature_request"
ACCOUNT_ACCESS = "account_access"
SECURITY_CONCERN = "security_concern"
class PriorityLevel(Enum):
LOW = "low"
NORMAL = "normal"
HIGH = "high"
URGENT = "urgent"
class EscalationType(Enum):
REFUND_REQUEST = "refund_request"
SUSPECTED_BREACH = "suspected_breach"
LEGAL_THREAT = "legal_threat"
PRODUCTION_OUTAGE = "production_outage"
class ProductArea(Enum):
AUTHENTICATION = "authentication"
PAYMENTS = "payments"
API = "api"
DASHBOARD = "dashboard"
INTEGRATIONS = "integrations"
INFRASTRUCTURE = "infrastructure"
These enumerations enforce valid state transitions. A ticket cannot be assigned an arbitrary priority string; it must select from the defined hierarchy. This constraint eliminates an entire class of runtime errors caused by typos or model hallucinations producing invalid category names.
Step 2: Model Account and Message Entities
Individual tickets exist within the context of a customer account and consist of one or more messages. Capture this structure explicitly to preserve metadata required for accurate triage.
from dataclasses import dataclass
from datetime import datetime
from typing import List
@dataclass
class CustomerAccount:
account_id: str
plan_tier: str
primary_contact_email: str
created_at: datetime
@dataclass
class TicketMessage:
message_id: str
timestamp: datetime
author_email: str
body_text: str
is_customer_initiated: bool
The CustomerAccount type holds subscription context. This influences priority scoring. Enterprise plan tickets often receive higher priority than free tier inquiries. The TicketMessage type isolates content from metadata, enabling redaction of body_text while preserving audit trails in the other fields.
Step 3: Aggregate the Support Ticket
Combine account context with message history to form the aggregate root of the domain. This entity represents the complete input to the triage workflow.
@dataclass
class SupportTicket:
ticket_id: str
account: CustomerAccount
messages: List[TicketMessage]
product_area: ProductArea = None
created_at: datetime = None
def get_conversation_text(self) -> str:
sorted_messages = sorted(self.messages, key=lambda m: m.timestamp)
return "\n\n".join(
f"[{m.timestamp.isoformat()}] {m.author_email}: {m.body_text}"
for m in sorted_messages
)
The get_conversation_text method serializes the thread for model consumption. Chronological ordering ensures the Gemma 4 model receives the correct temporal context when analyzing multi-turn support threads.
Step 4: Define Extracted Fields Structure
Structured extraction requires a typed schema rather than an opaque dictionary. Define the fields the model will attempt to parse from ticket text.
from dataclasses import dataclass, field
from typing import List, Optional
@dataclass
class ExtractedFields:
account_id: Optional[str] = None
plan_tier: Optional[str] = None
affected_feature: Optional[str] = None
outage_signals: List[str] = field(default_factory=list)
requested_action: Optional[str] = None
This schema documents the expected keys for downstream consumers. Using a dataclass instead of a dict provides autocomplete support and runtime type checking.
Step 5: Structure Triage Outputs
Define the result types that the model pipeline will produce. These structures enforce the constraint that escalations and replies require human approval.
The TriageResult type captures the complete analysis output. Structured data parsed from unstructured text lives in extracted_fields. Separating EscalationDecision and ReplyDraft into distinct types clarifies the approval workflow. Some tickets escalate without drafting a reply; others draft replies without escalation.
Step 6: Record Audit and Evaluation Data
Operational transparency requires tracking redaction actions, model execution metadata, and evaluation benchmarks. These types support compliance and continuous improvement.
RedactionRecord provides an audit trail for privacy compliance, documenting which fields were sanitized, which categories of PII were detected (email, phone, account_number), and what replacement token was used. ModelRun captures the provenance of every inference operation. It records model version, latency metrics, and the specific ticket processed. EvaluationExample pairs ground-truth labels with model outputs, enabling quantitative comparison between prompt-only, LoRA, and QLoRA variants in later chapters.
Step 7: Configure Safety Boundaries and Versioned Assets
Centralize project-wide settings and safety rules in a configuration type. Load this from versioned assets so operations teams can adjust behavior without code deployment.
The max_input_tokens field specifies the maximum token count for the combined prompt and conversation text. This measures tokens, not characters. Externalizing paths to prompt_template_path and label_schema_path ensures the system loads versioned assets at runtime. The from_yaml class method enables configuration changes without code redeployment.
Step 8: Define the TriageService Boundary
Establish a protocol that isolates Gemma 4 implementation details from business logic. This boundary owns model invocation but not workflow decisions.
The TriageService protocol defines the interface. Gemma4TriageService provides a concrete stub that you'll flesh out once you implement inference. Business logic depends on the protocol, not the concrete class, allowing you to swap implementations for testing or model upgrades.
Full Example
The following module consolidates the domain model into a single importable package. This structure establishes the type system used throughout the book's implementation of the support ticket triage assistant. It includes the versioned asset paths required for runtime configuration.
This domain model enforces architectural boundaries. The TriageResult type requires an EscalationDecision which defaults to requires_human_approval=True. This structure prevents accidental automation of sensitive decisions. The SupportTicket aggregate provides a clean interface for the TriageService boundary, isolating Gemma 4-specific implementation details from business logic. Versioned asset paths in TriageConfig ensure reproducible behavior across deployments.
Common Mistakes
Using strings instead of enums for classification labels. Enums raise AttributeError at runtime when accessed incorrectly, preventing invalid values from entering the pipeline. String typos pass silently until they cause logic errors downstream. Always use enums for categorical data.
Storing PII in training datasets without redaction. Never store unredacted PII in training datasets. Always generate a RedactionRecord before persisting any ticket content to disk. During a compliance audit last year, missing redaction records forced us to discard three months of training data. Don't skip the audit trail.
Skipping redaction audit trails. Most teams skip redaction audit trails. That's how PII leaks go undetected.
Omitting the `requires_human_approval` flag on automated replies. Models draft text that appears authoritative. Without explicit flags forcing human review, operators may accidentally send incorrect or unsafe responses to customers. Set requires_human_approval=True by default in all draft constructors.
Conflating model outputs with final business decisions. The TriageResult type represents a model prediction, not an operational decision. Maintain a strict boundary between the TriageService (which calls the model) and the workflow engine (which acts on results). Never let the model trigger actions directly.
Hardcoding escalation thresholds in conditional logic. Scattering keywords like "refund" or "legal" throughout if-statements makes the system brittle. Centralize these in TriageConfig and version the configuration file alongside model artifacts. Hardcoded safety rules are technical debt.
Performance and Production Considerations
Enum instantiation overhead is negligible compared to string validation. Prefer type safety over micro-optimizations.
Dataclass creation is lightweight. Don't replace these structures with dictionaries to save memory; the loss of type safety introduces maintenance costs that exceed any performance gain.
Redaction must occur before any data touches persistent storage. Scan for PII in the ingestion layer, not the training pipeline. Data reaching the training stage must already be clean.
Version all configuration assets with the model artifacts. A TriageConfig change alters system behavior as significantly as a model weight update. Store config hashes alongside model run metadata to ensure reproducibility.
GPU memory constraints determine viable Gemma 4 variants. The 31B dense model requires substantial VRAM for inference; the E4B variant runs efficiently on consumer hardware, and the 26B MoE model offers a strong middle ground with only 4B active parameters. Match model size to your deployment target early to avoid architectural rework.
Summary
Gemma 4 open-weight models enable private, customizable automation of support ticket triage. You maintain full data sovereignty while running advanced language models on your own infrastructure. The support ticket triage assistant built throughout this book demonstrates how to apply these capabilities within strict safety and privacy constraints.
The domain model established here provides the foundation. Type-safe enumerations prevent invalid classifications. Dataclasses enforce the rule that escalations and replies require human approval. Configuration objects externalize safety thresholds for operational control. The TriageService boundary isolates model specifics from business logic.
Apply these patterns when handling sensitive customer data requiring on-premise processing. Watch for PII leakage in training pipelines, maintain explicit escalation boundaries, and version all assets that influence model behavior. The architecture separates the TriageService boundary from underlying model implementations, allowing you to swap Gemma 4 variants or fine-tuned adapters without altering business logic.
Key Terms
Open-weight models: LLMs with publicly available weights that execute locally without external API dependencies, enabling full data sovereignty.
Gemma 4: Google's family of open-weight language models released in multiple parameter sizes and training configurations.
Support ticket triage assistant: The book's sample application that automates classification, prioritization, summarization, and response drafting for B2B SaaS customer support.
TriageService boundary: The architectural interface isolating Gemma 4 implementation details from business logic, allowing model swaps without workflow changes.
Instruction-tuned variants: Models fine-tuned on task demonstrations for immediate prompt-response execution without additional training.
Base models: Pre-trained foundations suitable for continued pre-training or custom instruction tuning on proprietary corpora.
RedactionRecord: Audit trail entity documenting PII removal actions, timestamps, and methods for privacy compliance.
ModelRun: Metadata structure capturing model version, adapter version, prompt version, latency, and token counts for operational transparency.
EscalationDecision: Structured output type enforcing human approval requirements for sensitive routing decisions.
Type-safe enums: Enumeration classes that reject invalid classification labels at instantiation time, catching errors immediately rather than allowing invalid strings to propagate.
Mini Quiz
What is the primary advantage of using open-weight models like Gemma 4 for support ticket processing?
A. They provide higher accuracy than closed API models on all tasks
B. They include built-in vendor-side content filters for customer safety
C. They require less computational resources than cloud API services
D. They enable full data sovereignty and on-premise processing of sensitive customer data
Consider the following Python code:
from enum import Enum
class IssueType(Enum):
BILLING = "billing"
BUG = "bug_report"
ticket_type = IssueType("refund")
What happens when this code executes?
A. It returns IssueType.BILLING
B. It returns None and logs a warning
C. It creates a new IssueType.REFUND member dynamically
D. It raises a ValueError because "refund" is not a defined member
An operations team receives a support ticket containing a credit card number in the message body and a request for an immediate refund. The automated classifier labels it as a feature request with low priority. What is the correct workflow according to the chapter's safety guidelines?
A. Process the low-priority feature request through the standard automated pipeline
B. Redact the PII, then immediately auto-reply with a refund confirmation to resolve the ticket quickly
C. Redact the PII, flag for human approval due to the refund request, and correct the misclassification
D. Delete the ticket to ensure PCI compliance and ask the customer to resubmit without sensitive data
Why does the TriageResult type enforce requires_human_approval=True on EscalationDecision and ReplyDraft?
A. To reduce API costs by limiting automated actions
B. To improve model inference throughput
C. To comply with Python dataclass inheritance requirements
D. To prevent accidental automation of sensitive customer decisions without oversight
What is the primary purpose of the RedactionRecord entity in the domain model?
A. To store encrypted customer passwords for authentication
B. To cache frequently accessed ticket content for performance
C. To track model prediction accuracy across different Gemma 4 variants
D. To provide an audit trail documenting PII removal for compliance
What distinguishes base models from instruction-tuned variants in the Gemma 4 family?
A. Base models are always smaller and faster than instruction-tuned variants
B. Instruction-tuned variants require proprietary API access to function
C. Base models suit continued pre-training; instruction-tuned variants offer immediate task execution
D. Instruction-tuned variants cannot be fine-tuned further with LoRA or QLoRA
Answers are provided in the Answer Key at the end of the book.
Activity
Implement privacy-safe ticket preparation by extending the domain model with redaction capabilities.
Create a PIIRedactor class with methods redact_email, redact_phone, and redact_account_number that use regular expressions to identify and replace sensitive patterns with [REDACTED].
Add a redact_messages method to the SupportTicket class that returns a new SupportTicket instance with all message bodies processed through the redactor, preserving the original timestamps and metadata.
Implement a validation function validate_ticket_for_inference that raises ValueError if the ticket contains no messages, if the conversation text exceeds max_ticket_length from TriageConfig, or if unredacted PII patterns are detected.
Write a script that instantiates a sample SupportTicket with realistic PII in the message bodies, applies redaction, generates a RedactionRecord documenting the fields removed, and outputs the sanitized conversation text suitable for Gemma 4 input.
Build a production-ready AI system with Google's most capable open-weight models. No API keys. No vendor lock-in. Your data stays on your hardware.
Google's Gemma 4 family gives you state-of-the-art reasoning in models you can download, inspect, and deploy anywhere. This book shows you exactly how to use them.
You will build one continuous project across all 14 chapters: a support ticket triage assistant that classifies issues, assigns priorities, generates summaries, extracts structured data, drafts replies, and flags escalations. Every code example connects to a real system you can extend and deploy.
What you will build: - A domain model with strict type safety, enums, and validation
- Tokenization and chat template pipelines for Gemma 4's E4B, 26B MoE, and 31B dense variants
- Inference services with batched generation, decoding strategies, and JSON extraction
- Prompt engineering workflows with versioned templates and A/B evaluation
- A full triage pipeline: ingestion, PII redaction, classification, and escalation routing
- LoRA and QLoRA fine-tuning on your own labeled ticket data
- Evaluation harnesses with statistical rigor (McNemar's test, confusion matrices, failure mode analysis)
- Quantized inference with INT8 and NF4 for deployment on constrained hardware
- A FastAPI service with health checks, graceful shutdown, and artifact versioning
What makes this book different: - One project, start to finish. No disconnected toy examples. Each chapter advances the same system from first inference to deployed service.
- Open weights, full control. Gemma 4 runs on your infrastructure. Process sensitive customer data without sending it to a third-party API.
- Production-first mindset. Memory estimation before model loading. Left-padding for batched generation. Temporal splits for honest evaluation. Safety architecture you own.
- Written by a practitioner. Hard-won lessons from real pipelines, not repackaged documentation.
Covers Gemma 4 models released April 2026, including the E4B edge model (128K context, 12GB VRAM), the 26B Mixture-of-Experts model (256K context, only 4B active parameters), and the 31B dense model for maximum accuracy.
Prerequisites: Intermediate Python. Basic familiarity with PyTorch tensors. No prior NLP or transformer experience required.
Stop paying per token. Start building AI systems you actually own.
Release DateApril 10, 2026
GenreSoftware & AI Development
ISBN979-8-90377-030-4
Read and listen on the go with the unQbd app.Get the app