Advanced Python Features

Summary: Top 18 Advanced Python Concepts #ConceptLocationWhy Important1Async Context Managerclient.py:55-78Resource lifecycle2Dataclass field()types.py:37Mutable defaults3post_inittypes.py:132Auto-validation4Enum(str, Enum)types.py:17JSON-serializable enums5Union (AB)client.py:366isinstance type guardsclient.py:144Type narrowing7Literal typestypes.py:84Exact value constraints8Callable/Awaitabletypes.py:90Async callback types9Private methods (_)client.py:198API encapsulation10Defensive copyclient.py:333Prevent mutation11Error-as-valueclient.py:141-192Railway-oriented design12@staticmethodengine.py:20Pure functions13dict.get()engine.py:84Safe access14perf_counter()client.py:139High-precision timing15Observer patternclient.py:198-239Hook system16Module structureinit.pySeparation of concerns17Relative importsclient.py:12Package portability18Builder patterntypes.py:104Flexible configuration One by one analysis: First Async Context Manager for example class CalculatorClient: def … Continue reading Advanced Python Features

Types

This post is to cover a comprehensive, intuitive set of advanced typing primitives that show up in modern Python (≥3.9). These types enable:- Type safety without runtime overhead- Better IDE autocomplete- Self-documenting APIs- Generic/reusable code To read below codes, need to know Type hints are ignored by Python’s interpreter — they exist only for static … Continue reading Types

A Practical Guide to Building Agents by OpenAI

This is the guide provided by OpenAI to help use agents SDK. Several key points cited in the following. First, write good prompts to configure, for example use advanced models such as o3-mini to write "“You are an expert in writing instructions for an LLM agent. Convert the following help center document into a clear … Continue reading A Practical Guide to Building Agents by OpenAI

Interactive Conversation to Perform Tasks is Essential in This Agent

The OpenAI Agent SDK provides the foundation (agents, tools, MCP, sessions), but we need a custom interactive wrapper to handle the confirmation/correction flow that's essential. current simple_agent.py does this beautifully:# Line 1149-1176: Interactive parameter correctionprint(f"\nExecute tool '{tool_name}' with these parameters? (y/n): ")user_input = input().strip()if user_input == 'n':# User can provide corrections# Agent remembers and retries … Continue reading Interactive Conversation to Perform Tasks is Essential in This Agent

Comparing My Agent to OpenAI Agent SDK

My agent hand-rolls LLM prompting, tool selection, conversational memory, and MCP tool discovery while openai-agents-python/src/agents/agent.py already encapsulates these behaviors through the Agent runtime. Custom MCP plumbing, the class manages ClientSession, output parsing, retries, and tool enablement logic on its own, while agents.mcp.MCPUtil and Agent.get_all_tools() can surface MCP tools automatically once servers are connected. Conversation history and logging live in plain dicts and prints. The … Continue reading Comparing My Agent to OpenAI Agent SDK

Cook Book Review: Agent SDK Agent as a Portfolio Manager

This complex agent is meant to create a workflow that multiple specialist agents (Macro, Fundamental, Quantitative) collaborate under a Portfolio Manager agent to solve a challenging investment research problem. Workflow codes: import datetime import json import os from pathlib import Path from contextlib import AsyncExitStack from agents import Runner, add_trace_processor, trace from agents.tracing.processors import BatchTraceProcessor … Continue reading Cook Book Review: Agent SDK Agent as a Portfolio Manager