{"slug": "ai-agents-async-programming-and-pytantic-data-validation", "title": "AI Agents - Async Programming and Pytantic Data Validation", "summary": "A developer outlined how Python's asyncio enables non-blocking I/O for AI agent workflows and how the Pydantic library enforces structured data validation on unstructured LLM text output. The writeup walks through synchronous versus asynchronous function execution and shows a BaseModel example where a Field constraint requires age to be greater than zero.", "body_md": "Sync means one after another. For example, calling 3 functions named `f1`, `f2`, and `f3`. Assume `f1` performs some I/O operations like a DB query, API request, or file operation. After `f1` is completed, `f2` and `f3` will be executed.\n\nThere are some operations that are running on top of the CPU.\n\nE.g.\n\n``` python\ndef add(a, b):\n    return a + b\n```\n\nWhen we call this function, it will be executed by the CPU.\n\nAnd some operations do not utilize the CPU, i.e., I/O operations.\n\n``` python\ndef getweatherdetails():\n    return request to HTTP API\n```\n\nHere, to send a request, the CPU will be utilized. After sending the request, the CPU will not be used until we get the response from the third party. We need to wait for some time.\n\nIn this case, if the main function is calling all 3 functions one by one, we have to wait for `f1` to get the response before moving to `f2`, and so on.\n\n``` python\ndef main():\n\n    f1()\n    f2()\n    f3()\n```\n\nHere, in Async, after sending the request to the third party, `f2` will be performed without waiting for the response from the third party. Async means no waiting (context switching) and will be used only in I/O operations.\n\nAsync will be implemented in Python using a package called `asyncio`.\n\nHere, `make_toast()` will be called first, and during the waiting time, `make_tea()` will be called.\n\nBy default, the output that comes from the LLM will not be structured. It will be in text format. Either we should structure it by writing code or use a prompt that is fed into the LLM so that the output can be used to take further action.\n\nPython is a dynamically typed language.\n\n```\nage = 12\nage = \"12\"\n```\n\nHere, the data type of the variable `age` is converted from `int` to `string`. Even though there are many benefits to dynamic typing, some problems may arise.\n\nThe response from the LLM will be in text format. So, we have to parse it using a regular expression or get the output in the desired format by creating a data structure. So, the LLM must adhere to the format to generate the output.\n\nExample:\n\nData structure called:\n\n```\n{\n    \"name\": \"string\",\n    \"age\": \"int\"\n}\n```\n\nSo, we can access the name by using `user.name`.\n\nBefore accessing it, we have to do validation to check the correctness. Data validation will not be applied implicitly to control the data. It will increase the dumb code, which affects the readability and maintainability of the code.\n\nTo solve this problem, there is a package called Pydantic. This package will control the data validation. So, we can totally rely on the objects that are produced by Pydantic.\n\n``` python\nfrom pydantic import BaseModel, Field\n\nclass User(BaseModel):\n    name: str\n    age: int = Field(gt=0)\n\nuser = User(name=\"somename\", age=30)\nprint(user)\n```\n\nTo add more implicit conditions, we have to import the `Field` class. Here, we are saying that `age` should be greater than 0.", "url": "https://wpnews.pro/news/ai-agents-async-programming-and-pytantic-data-validation", "canonical_source": "https://dev.to/ramya_perumal/ai-agents-async-programming-and-pytantic-data-validation-o44", "published_at": "2026-10-01 18:35:32+00:00", "updated_at": "2026-10-01 18:44:45.181728+00:00", "lang": "en", "topics": ["ai-agents", "large-language-models", "developer-tools", "natural-language-processing"], "entities": ["Python", "asyncio", "Pydantic"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/ai-agents-async-programming-and-pytantic-data-validation", "markdown": "https://wpnews.pro/news/ai-agents-async-programming-and-pytantic-data-validation.md", "text": "https://wpnews.pro/news/ai-agents-async-programming-and-pytantic-data-validation.txt", "jsonld": "https://wpnews.pro/news/ai-agents-async-programming-and-pytantic-data-validation.jsonld"}}