Posts

python pydantic

Pydantic is a popular library which we can use for defining our custom objects and making it so much easy to use for day to day coding.  Here is one of the example that i link can really boost productivity - this is how we can assign data into our objects. from pydantic import BaseModel , EmailStr , Field class UserRegistration ( BaseModel ):     username : str = Field ( min_length = 3 , max_length = 20 )     age : int = Field ( ge = 18 )   # Must be 18 or older # Incoming untrusted JSON data raw_input = { "username" : "alice_dev" , "email" : "alice@example.com" , "age" : "25" } # Pydantic validates and coerces data types automatically user = UserRegistration ( ** raw_input ) print ( user . age )   # Output: 25 (converted from string to int) Application settings To work with an app config that automatically read from DATABASE_URL and REDIS_URL environment variables. Pre-req, you need to install  uv add pydantic_set...

python using orjson to work with json

orjson is a json library that is fast and getting popular.  Here are example code to work with same data  import orjson from dataclasses import dataclass from datetime import datetime @ dataclass class User :     id : int     name : str     created_at : datetime user = User ( id = 1 , name = "Alice" , created_at = datetime . now ()) # --- SERIALIZATION (Python Object -> JSON bytes) --- json_bytes = orjson . dumps ( user ) print ( json_bytes ) # Output: b'{"id":1,"name":"Alice","created_at":"2026-10-04T15:12:00+00:00"}' # --- DESERIALIZATION (JSON bytes/str -> Python Dict) --- data = orjson . loads ( json_bytes ) print ( data ) # Output: {'id': 1, 'name': 'Alice', 'created_at': '2026-10-04T15:12:00+00:00'} print(data['id']) # output 1

python async library - recommended way to wait for task completion

When using async library, it is always good to use TaskGroup to manage execution of different tasks where we use create_task to create async task.  import asyncio import time async def worker ( task_id : int , delay : int ):     print ( f "Task { task_id } started (sleeping { delay } s)..." )     await asyncio . sleep ( delay )     print ( f "Task { task_id } finished after { delay } s!" )     return f "Result { task_id } " async def main ():     start_time = time . time ()         # TaskGroup manages execution and waits until all tasks finish     async with asyncio . TaskGroup () as tg :         t1 = tg . create_task ( worker ( 1 , 1 ))         t2 = tg . create_task ( worker ( 2 , 2 ))         t3 = tg . create_task ( worker ( 3 , 10 ))         # All tasks are guaranteed complete after exiting the contex...

adk getting it to deploy to google agentic platform - errors

Image
I was trying to deploy using this command and pretty sure agentic platform is available here in Australia South East 2 (Melbourne) and I wasn't try to deploy to Sydney.  uv run adk deploy agent_engine agent_togo --project="project-015f2405-16e7-4b36-996" --region="australia-southeast2" Bump into this error when trying to deploy my adk sample app to google agentic platform.  To be fair, I didn't have my gcloud cli setup on my laptop. So I fixed that with  gcloud auth application-default login Deploy failed: ('invalid_grant: Bad Request', {'error': 'invalid_grant', 'error_description': 'Bad Request'}) Then bump into this error which is a generic error. And that drove me to check Log Explorer to find out more details Deploy failed: Failed to create Agent Engine: {'code': 10, 'message': 'Please refer to our troubleshooting pages (e.g., https://docs.cloud.google.com/gemini-enterprise-agent-platform/troub...

gcloud cli - getting, setting and working with projects

  You can get the info and listing of your proect using the following command  To show your local config gcloud config list To list project gcloud projects list To set project Id gcloud config set project PROJECT_ID  To set and configure billing quota gcloud config set billing/quota_project YOUR_REAL_PROJECT_ID  

commander using Qwen3.8-2B-Distill-GGUF model

Image
This is my main.py which uses the  Qwen3.8-2B-Distill-GGUF model. And the instruction is pretty much the same as previous model how ever, it does not seems to 'want' to read from start until end.  And because of that, the results is not so accurate.  """Have the model READ a README, detect its runtime, and extract the commands. Then start a matching container, run each command in it, and stop it. Usage:     python run_agent.py path/to/README.md """ import argparse import asyncio import inspect import json import re import shlex from pathlib import Path from dotenv import load_dotenv from google . adk . agents import LlmAgent from google . adk . runners import Runner from google . adk . sessions import InMemorySessionService from google . genai import types from pydantic import BaseModel , Field load_dotenv () # Reuse the same model your commander agent uses. from app . agent import root_agent   # ASSUMPTION: plain fu...

commander using llm model openai/XHToken/Spark-X2.5-4B-GGUF

Image
When running my commander app with XHToken/Spark-X2.5-4B-GGUF the result is pretty good. The model is able to follow instruction and extracts out the right container image to run and the commands too.   This is my codebase for setting up my model XHToken/Spark-X2.5-4B-GGUF. import os from dotenv import load_dotenv from google . adk . agents import LlmAgent from google . adk . models . lite_llm import LiteLlm from google . adk . tools import google_search # Create a LiteLLM model pointing to your local server model = LiteLlm (     model = "openai/XHToken/Spark-X2.5-4B-GGUF" ,     api_base = "http://localhost:8888/v1" ,   # Your local server     api_key = "sk-unsloth-4d0a1b198bd177a2a72ee1954585342a" ,     temperature = 0.0 ,     extra_body = { "chat_template_kwargs" : { "enable_thinking" : False }}, ) from app . prompt import ROOT_AGENT_INSTRUCTION from app . tools . container_toolset import (   ...

commander - using ornith-ai/Ornith-1.0-9B-GGUF as the model and documenting the results

Image
 After trying out a couple of model, I decides to see if using a different model would sway the results differently. I am using ornith-ai/Ornith-1.0-9B-GGUF. model here.  Noticed that I have added "openai" in front of the model name otherwise it throw an error message. import os from dotenv import load_dotenv from google . adk . agents import LlmAgent from google . adk . models . lite_llm import LiteLlm from google . adk . tools import google_search # Create a LiteLLM model pointing to your local server model = LiteLlm (     model = "openai/ornith-ai/Ornith-1.0-9B-GGUF" ,     api_base = "http://localhost:8888/v1" ,   # Your local server     api_key = "sk-unsloth-4d0a1b198bd177a2a72ee1954585342a" ,     temperature = 0.0 ) from app . prompt import ROOT_AGENT_INSTRUCTION from app . tools . container_toolset import (     container_image_finder_tool , run_container_command_tool ) load_dotenv () root_agent ...

Quickstart with laya

Image
To get started with laya.  Laya is a free, open-source AI model designed for fast, focused text classification. It makes simple decisions—such as routing emails, prioritising tickets, or identifying refund requests—by choosing from predefined options and providing a confidence score. In short: Laya is built for fast, low-cost AI decisions rather than generating text. To get started, we have to install laya and creating your python environment. Next we going to use it to decide if the ticket belongs to which department. This is an approach where it uses the model with your local computing resources. There's another option for you use Laya studio which requires an API but you have to sign up first. import laya from laya import Router # Initialize router with preloading (avoids swap delay) router = Router ( preload = True ) # Define complex state ticket = {     "ticket_id" : "TCK-8821" ,     "customer" : "enterprise_user" ,     "su...