Skip to content

Content Generation Agent with Pydantic AI

Build an AI content generation agent that researches topics, writes blog posts and social media content with consistent brand voice — using Pydantic AI.

content blog social media writing

Working Code

Pydantic AI
from pydantic_ai import Agent, RunContext
agent = Agent(
"openai:gpt-4o",
system_prompt="You are a content writer. Research the topic first, then write engaging content. Save the final output using save_content.",
)
@agent.tool
async def web_search(ctx: RunContext, query: str) -> str:
"""Research a topic before writing content."""
from tavily import TavilyClient
client = TavilyClient()
results = client.search(query, max_results=3)
return "\n\n".join(r["content"] for r in results["results"])
@agent.tool
async def save_content(ctx: RunContext, filename: str, content: str) -> str:
"""Save generated content to a file."""
Path(f"output/{filename}").write_text(content)
return f"Saved to output/{filename}"
result = await agent.run("Write a blog post about the benefits of AI agents in customer service")
print(result.output)

Step by Step

1

Install dependencies

Install Pydantic AI and the required tools for this use case.

2

Define your tools

Create the domain-specific tool functions your agent will use to interact with external services.

3

Create the agent and run

Initialize the Pydantic AI agent with your tools, set the system prompt, and execute a query.

Ready to build with Pydantic AI?

Generate a production-ready project with Pydantic AI pre-configured — FastAPI + Next.js, auth, streaming, and more.

Get Started

Ready to build your first production AI agent?

Open-source tools, battle-tested patterns, zero boilerplate. Configure your stack and ship in minutes — not months.