ast.parse() converts source code into an abstract syntax tree. Linters, migration tools, and analyzers walk nodes to understand structure without importing the analyzed module.
Practical example
import ast
source = "{'retries': 3, 'tags': ['api', 'stable']}"
value = ast.literal_eval(source)
if not isinstance(value, dict):
raise TypeError("expected a dictionary")
print(value["retries"])
literal_eval is not eval
ast.literal_eval() accepts only literal structures such as strings, numbers, lists, and dictionaries. Validate type and schema afterward; JSON is usually better for interchange between systems.
No execution does not mean no risk
Very deep or large input can consume memory and stack space. Limit bytes and depth before analyzing external content. Never compile or execute a received AST without a strict policy and proper isolation.
Keep learning
Continue with Python JSON: Complete Guide to Data Manipulation and Python Type Hints: Complete Static Typing Guide. A official Python documentation, accessed July 22, 2026, documents the API and behavior across versions.