Sub topic 13
Choosing an approach for your project
This chapter assumes you can call a chat API from Python, read Pydantic models, and know what retrieval-augmented generation (RAG) is. It also assumes you have seen the earlier chapters on structured output, tool calling, and what frameworks add and cost. Here you turn that knowledge into a decision you can defend.
Why the choice is a trade-off, not a ranking
No option wins in every case. A provider SDK gives you the most control and the least magic. A framework removes code you would otherwise write, and in exchange you accept its abstractions, its dependencies, and its release schedule.
Four options are in play:
- Provider SDK only, such as the
openaipackage, plus Pydantic and ordinary Python. - LangChain, which offers composable components and many integrations.
- LlamaIndex, which centers on loading, indexing, and querying your own data.
- PydanticAI, which builds typed agents on top of Pydantic validation.
You can also mix them. Most real projects end up doing so, and the last section covers how.
The criteria
Judge a project on six criteria. Each is a question you can answer with evidence from your own code and team.
Portability
Will you need more than one model provider? If you might switch providers or run several side by side, a common interface saves work. LangChain's chat model wrapper and PydanticAI's model strings such as openai:gpt-4o-mini both let you change providers by changing one name.
Portability has limits. Providers differ in how they handle tool calls, structured output modes, token limits, and streaming. A wrapper hides these differences until one of them matters. Then you read the wrapper's source to find out what it did.
If you will only ever use one provider, portability is worth little. Many OpenAI-compatible servers also accept the openai client with a different base_url, which gives a cheap form of portability without a framework.
Control
How much of the request do you need to shape? A raw SDK call shows every message, parameter, and retry. Frameworks add prompt templates, message conversion, output parsers, and retry logic. These help when they match your need. They get in the way when you need an unusual request shape, such as a custom ordering of messages or provider-specific parameters.
Debuggability
When an answer is wrong, you must see what the model was actually sent. With an SDK, you print the messages list. With a framework, the final prompt may be assembled several layers down. Frameworks usually offer callbacks, tracing, or logging hooks, and you should plan to use them from the start. If you cannot inspect the final request, you cannot debug the system.
Maintenance risk
Framework APIs change. Class names move between packages, old chains are deprecated, and examples from a year ago may not run. Dependency weight matters too: a framework pulls in many packages, which raises the odds of version conflicts and security updates. A provider SDK also changes, but its surface is smaller and tied to one vendor's documented API.
Pin versions, read changelogs before upgrading, and keep framework calls behind a thin layer of your own code. The same applies to the PydanticAI example in this tutorial, which uses output_type and result.output from recent releases. Earlier versions used different names.
Task fit
Match the tool to the main job.
- Structured output with validation points to Pydantic, either by hand or through PydanticAI.
- Document-heavy RAG points to LlamaIndex, which turns loading, chunking, embedding, indexing, and querying into a few lines.
- Many integrations (vector stores, loaders, model providers) point to LangChain.
- A single call or a short fixed sequence points to the SDK.
Team and lifetime
A prototype that lives two weeks has different needs from a service that a rotating team maintains for years. Frameworks give newcomers a shared vocabulary and ready examples. Raw SDK code is easier to read for anyone who knows Python and the vendor's docs, and it has fewer concepts to learn.
A comparison table
| Option | Core design focus | Strongest when | Main cost |
|---|---|---|---|
| Provider SDK | Direct access to one vendor's API | Few calls, full control, one provider | You write boilerplate, retries, and loops yourself |
| LangChain | Composable components and integrations | Many providers, stores, and tools to connect | Large API surface and frequent change |
| LlamaIndex | Data ingestion, indexing, and querying | RAG over many documents and formats | Defaults hide choices such as chunk size and the default OpenAI models |
| PydanticAI | Typed, validated agents | Reliable structured results and typed tools | Younger API, changes between releases |
A decision flow
The diagram gives a starting point. Treat each answer as a lean, not a rule.
Worked examples
Example 1: A ticket classifier
A support tool must read a message and return a category and an urgency from 1 to 5. This is one call with a validated result. The SDK with Pydantic does the job in about fifteen lines, as in the structured output chapter. A framework adds nothing here except a dependency.
Choice: provider SDK plus Pydantic. If you later want automatic retries when validation fails, PydanticAI is a small step up, since it validates against your model and returns a typed object.
Example 2: A question-answering bot over company documents
The team has several hundred PDFs and wiki pages. They need loading, chunking, embedding, storage, and querying. Writing this by hand is possible, and you did it in the RAG chapter, but the loaders and chunking strategies take real time to build and test.
Choice: LlamaIndex for the first version. The five-line pipeline gets a working baseline quickly. Then inspect what it does: chunk size, the embedding model, and the number of retrieved chunks. Tune those settings before you judge the quality of the answers.
Example 3: An internal agent that calls many services
The agent needs a language model, a vector store, three internal APIs as tools, and a switch between two providers for cost reasons. The integration list is long, and the team wants tracing.
Choice: LangChain or PydanticAI, depending on what dominates. If the provider and store integrations dominate, LangChain saves time. If typed tools and validated outputs dominate, PydanticAI fits better. Keep your own business logic in plain functions so either choice stays replaceable.
Common mistakes
- Choosing by popularity. A tool being widely used does not mean it fits your task.
- Adding a framework for one call. The dependency cost is paid on every upgrade, while the savings are a few lines.
- Rewriting what a framework already does well. Hand-built document loaders for ten file formats cost more than the dependency.
- Skipping observability. With a framework you cannot see the prompt unless you set up tracing. With an SDK you can still lose track of it if you never log requests.
- Letting framework types leak everywhere. If every function in your code takes a framework message class, you cannot leave later.
- Deciding once and never revisiting. Requirements change. Revisit the choice when the project's main job changes.
A quick scoring method
When the choice is not obvious, score each option from 1 to 5 on each criterion, weighted by how much that criterion matters to the project. The numbers are crude, but writing them forces you to state your reasons.
CRITERIA_WEIGHTS = {
"portability": 2,
"control": 3,
"debuggability": 3,
"maintenance": 2,
"task_fit": 4,
}
SCORES = {
"sdk": {"portability": 2, "control": 5, "debuggability": 5, "maintenance": 4, "task_fit": 3},
"langchain": {"portability": 5, "control": 3, "debuggability": 3, "maintenance": 2, "task_fit": 4},
"llamaindex": {"portability": 3, "control": 3, "debuggability": 3, "maintenance": 3, "task_fit": 5},
"pydanticai": {"portability": 4, "control": 4, "debuggability": 4, "maintenance": 3, "task_fit": 4},
}
def total(option: str) -> int:
return sum(
CRITERIA_WEIGHTS[name] * SCORES[option][name]
for name in CRITERIA_WEIGHTS
)
for option in sorted(SCORES, key=total, reverse=True):
print(f"{option}: {total(option)}")The scores above are made-up inputs for one imaginary project. Replace them with your own judgments and weights. If two options finish within a few points of each other, pick the simpler one, because you can move later.
Summary of the decision
- Start from the main job, not the tool.
- Default to the SDK plus Pydantic for small, fixed tasks.
- Add a framework when it removes real work you would otherwise write and test.
- Set up tracing before the first bug, whichever option you choose.
- Isolate the model call behind your own function so a later change stays local.
Exercise
Your team builds a service that extracts an invoice number and total from emails. It makes one model call per email, uses one provider, and must return a validated object. A teammate proposes LangChain "for flexibility". Which option would you choose, and which two criteria from this chapter support your answer?
Show answer
Choose the provider SDK with a Pydantic model (or PydanticAI if you want automatic validation and retries). The job is one call with a validated result, so task fit favors the SDK. Only one provider is used, so portability is worth little. Maintenance risk also favors the smaller dependency surface, because a framework would add packages and API churn for no matching benefit.
Exercise
A project needs to answer questions over 400 mixed PDF and HTML files and may switch from one model provider to another next quarter. Name a sensible starting choice, one setting you must inspect before judging answer quality, and one step that keeps a later switch cheap.
Show answer
Start with LlamaIndex for the document pipeline. Inspect the chunk size, the embedding model, or the number of retrieved chunks, because defaults hide these and they strongly affect answer quality. To keep a later switch cheap, wrap the model call in your own function so business code does not depend on framework types.