Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      The Value-Driven AI Roadmap

      September 9, 2025

      This week in AI updates: Mistral’s new Le Chat features, ChatGPT updates, and more (September 5, 2025)

      September 6, 2025

      Designing For TV: Principles, Patterns And Practical Guidance (Part 2)

      September 5, 2025

      Neo4j introduces new graph architecture that allows operational and analytics workloads to be run together

      September 5, 2025

      ‘Job Hugging’ Trend Emerges as Workers Confront AI Uncertainty

      September 8, 2025

      Distribution Release: MocaccinoOS 25.09

      September 8, 2025

      Composition in CSS

      September 8, 2025

      DataCrunch raises €55M to boost EU AI sovereignty with green cloud infrastructure

      September 8, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Finally, safe array methods in JavaScript

      September 9, 2025
      Recent

      Finally, safe array methods in JavaScript

      September 9, 2025

      Perficient Interviewed for Forrester Report on AI’s Transformative Role in DXPs

      September 9, 2025

      Perficient’s “What If? So What?” Podcast Wins Gold Stevie® Award for Technology Podcast

      September 9, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Distribution Release: MocaccinoOS 25.09

      September 8, 2025
      Recent

      Distribution Release: MocaccinoOS 25.09

      September 8, 2025

      Speed Isn’t Everything When Buying SSDs – Here’s What Really Matters!

      September 8, 2025

      14 Themes for Beautifying Your Ghostty Terminal

      September 8, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Machine Learning»A Coding Guide to Build an Agentic AI‑Powered Asynchronous Ticketing Assistant Using PydanticAI Agents, Pydantic v2, and SQLite Database

    A Coding Guide to Build an Agentic AI‑Powered Asynchronous Ticketing Assistant Using PydanticAI Agents, Pydantic v2, and SQLite Database

    April 22, 2025

    In this tutorial, we’ll build an end‑to‑end ticketing assistant powered by Agentic AI using the PydanticAI library. We’ll define our data rules with Pydantic v2 models, store tickets in an in‑memory SQLite database, and generate unique identifiers with Python’s uuid module. Behind the scenes, two agents, one for creating tickets and one for checking status, leverage Google Gemini (via PydanticAI’s google-gla provider) to interpret your natural‑language prompts and call our custom database functions. The result is a clean, type‑safe workflow you can run immediately in Colab.

    Copy CodeCopiedUse a different Browser
    !pip install --upgrade pip
    !pip install pydantic-ai

    First, these two commands update your pip installer to the latest version, bringing in new features and security patches, and then install PydanticAI. This library enables the definition of type-safe AI agents and the integration of Pydantic models with LLMs.

    Copy CodeCopiedUse a different Browser
    import os
    from getpass import getpass
    
    
    if "GEMINI_API_KEY" not in os.environ:
        os.environ["GEMINI_API_KEY"] = getpass("Enter your Google Gemini API key: ")

    We check whether the GEMINI_API_KEY environment variable is already set. If not, we securely prompt you (without echoing) to enter your Google Gemini API key at runtime, then store it in os.environ so that your Agentic AI calls can authenticate automatically.

    Copy CodeCopiedUse a different Browser
    !pip install nest_asyncio

    We install the nest_asyncio package, which lets you patch the existing asyncio event loop so that you can call async functions (or use .run_sync()) inside environments like Colab without running into “event loop already running” errors.

    Copy CodeCopiedUse a different Browser
    import sqlite3
    import uuid
    from dataclasses import dataclass
    from typing import Literal
    
    
    from pydantic import BaseModel, Field
    from pydantic_ai import Agent, RunContext

    We bring in Python’s sqlite3 for our in‑memory database and uuid to generate unique ticket IDs, use dataclass and Literal for clear dependency and type definitions, and load Pydantic’s BaseModel/​Field for enforcing data schemas alongside Agent and RunContext from PydanticAI to wire up and run our conversational agents.

    Copy CodeCopiedUse a different Browser
    conn = sqlite3.connect(":memory:")
    conn.execute("""
    CREATE TABLE tickets (
        ticket_id TEXT PRIMARY KEY,
        summary   TEXT NOT NULL,
        severity  TEXT NOT NULL,
        department TEXT NOT NULL,
        status    TEXT NOT NULL
    )
    """)
    conn.commit()
    

    We set up an in‑memory SQLite database and define a tickets table with columns for ticket_id, summary, severity, department, and status, then commit the schema so you have a lightweight, transient store for managing your ticket records.

    Copy CodeCopiedUse a different Browser
    @dataclass
    class TicketingDependencies:
        """Carries our DB connection into system prompts and tools."""
        db: sqlite3.Connection
    
    
    class CreateTicketOutput(BaseModel):
        ticket_id: str = Field(..., description="Unique ticket identifier")
        summary: str   = Field(..., description="Text summary of the issue")
        severity: Literal["low","medium","high"] = Field(..., description="Urgency level")
        department: str = Field(..., description="Responsible department")
        status: Literal["open"] = Field("open", description="Initial ticket status")
    
    
    class TicketStatusOutput(BaseModel):
        ticket_id: str = Field(..., description="Unique ticket identifier")
        status: Literal["open","in_progress","resolved"] = Field(..., description="Current ticket status")

    Here, we define a simple TicketingDependencies dataclass to pass our SQLite connection into each agent call, and then declare two Pydantic models: CreateTicketOutput (with fields for ticket ID, summary, severity, department, and default status “open”) and TicketStatusOutput (with ticket ID and its current status). These models enforce a clear, validated structure on everything our agents return, ensuring you always receive well-formed data.

    Copy CodeCopiedUse a different Browser
    create_agent = Agent(
        "google-gla:gemini-2.0-flash",
        deps_type=TicketingDependencies,
        output_type=CreateTicketOutput,
        system_prompt="You are a ticketing assistant. Use the `create_ticket` tool to log new issues."
    )
    
    
    @create_agent.tool
    async def create_ticket(
        ctx: RunContext[TicketingDependencies],
        summary: str,
        severity: Literal["low","medium","high"],
        department: str
    ) -> CreateTicketOutput:
        """
        Logs a new ticket in the database.
        """
        tid = str(uuid.uuid4())
        ctx.deps.db.execute(
            "INSERT INTO tickets VALUES (?,?,?,?,?)",
            (tid, summary, severity, department, "open")
        )
        ctx.deps.db.commit()
        return CreateTicketOutput(
            ticket_id=tid,
            summary=summary,
            severity=severity,
            department=department,
            status="open"
        )
    

    We create a PydanticAI Agent named’ create_agent’ that’s wired to Google Gemini and is aware of our SQLite connection (deps_type=TicketingDependencies) and output schema (CreateTicketOutput). The @create_agent.tool decorator then registers an async create_ticket function, which generates a UUID, inserts a new row into the tickets table, and returns a validated CreateTicketOutput object.

    Copy CodeCopiedUse a different Browser
    status_agent = Agent(
        "google-gla:gemini-2.0-flash",
        deps_type=TicketingDependencies,
        output_type=TicketStatusOutput,
        system_prompt="You are a ticketing assistant. Use the `get_ticket_status` tool to retrieve current status."
    )
    
    
    @status_agent.tool
    async def get_ticket_status(
        ctx: RunContext[TicketingDependencies],
        ticket_id: str
    ) -> TicketStatusOutput:
        """
        Fetches the ticket status from the database.
        """
        cur = ctx.deps.db.execute(
            "SELECT status FROM tickets WHERE ticket_id = ?", (ticket_id,)
        )
        row = cur.fetchone()
        if not row:
            raise ValueError(f"No ticket found for ID {ticket_id!r}")
        return TicketStatusOutput(ticket_id=ticket_id, status=row[0])

    We set up a second PydanticAI Agent, status_agent, also using the Google Gemini provider and our shared TicketingDependencies. It registers an async get_ticket_status tool that looks up a given ticket_id in the SQLite database and returns a validated TicketStatusOutput, or raises an error if the ticket isn’t found.

    Copy CodeCopiedUse a different Browser
    deps = TicketingDependencies(db=conn)
    
    
    create_result = await create_agent.run(
        "My printer on 3rd floor shows a paper jam error.", deps=deps
    )
    
    
    print("Created Ticket →")
    print(create_result.output.model_dump_json(indent=2))
    
    
    tid = create_result.output.ticket_id
    status_result = await status_agent.run(
        f"What's the status of ticket {tid}?", deps=deps
    )
    
    
    print("Ticket Status →")
    print(status_result.output.model_dump_json(indent=2))

    Finally, we first package your SQLite connection into deps, then ask the create_agent to log a new ticket via a natural‑language prompt, printing the validated ticket data as JSON. It then takes the returned ticket_id, queries the status_agent for that ticket’s current state, and prints the status in JSON form.

    In conclusion, you have seen how Agentic AI and PydanticAI work together to automate a complete service process, from logging a new issue to retrieving its live status, all managed through conversational prompts. Our use of Pydantic v2 ensures every ticket matches the schema you define, while SQLite provides a lightweight backend that’s easy to replace with any database. With these tools in place, you can expand the assistant, adding new agent functions, integrating other AI models like openai:gpt-4o, or connecting real‑world APIs, confident that your data remains structured and reliable throughout.


    Here is the Colab Notebook. Also, don’t forget to follow us on Twitter and join our Telegram Channel and LinkedIn Group. Don’t Forget to join our 90k+ ML SubReddit.

    🔥 [Register Now] miniCON Virtual Conference on AGENTIC AI: FREE REGISTRATION + Certificate of Attendance + 4 Hour Short Event (May 21, 9 am- 1 pm PST) + Hands on Workshop

    The post A Coding Guide to Build an Agentic AI‑Powered Asynchronous Ticketing Assistant Using PydanticAI Agents, Pydantic v2, and SQLite Database appeared first on MarkTechPost.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleDecoupled Diffusion Transformers: Accelerating High-Fidelity Image Generation via Semantic-Detail Separation and Encoder Sharing
    Next Article Researchers at Physical Intelligence Introduce π-0.5: A New AI Framework for Real-Time Adaptive Intelligence in Physical Systems

    Related Posts

    Machine Learning

    How to Evaluate Jailbreak Methods: A Case Study with the StrongREJECT Benchmark

    September 3, 2025
    Machine Learning

    Announcing the new cluster creation experience for Amazon SageMaker HyperPod

    September 3, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    CL-STA-0969 Installs Covert Malware in Telecom Networks During 10-Month Espionage Campaign

    Development

    CVE-2025-53603 – Alinto SOPE SOGo NULL Pointer Dereference

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-36560 – A-Blog CMS SSRF Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Ecosystem Partnerships: Driving Mainframe Innovation and Future-Ready Solutions

    Databases

    Highlights

    Fog Ransomware Directory With Active Directory Exploitation Tools & Scripts Uncovered

    April 29, 2025

    Fog Ransomware Directory With Active Directory Exploitation Tools & Scripts Uncovered

    Cybersecurity analysts have uncovered an open directory linked to the Fog ransomware group, revealing a comprehensive toolkit used by threat actors to compromise corporate networks.
    The directory, dis …
    Read more

    Published Date:
    Apr 28, 2025 (20 hours, 17 minutes ago)

    Vulnerabilities has been mentioned in this article.

    CVE-2021-42287

    CVE-2021-42278

    CVE-2020-1472

    Microsoft may end high-stakes OpenAI talks, and keep the current contract

    June 21, 2025

    CVE-2025-54474 – “DJ-Classifieds SQL Injection Vulnerability”

    August 15, 2025

    How to Save and Share Flutter Widgets as Images – A Complete Production-Ready Guide

    September 3, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.