Sub topic 14
Migrating and mixing approaches
This chapter assumes you have read the earlier chapters on provider SDK calls, structured output, tool calling, and the framework trade-offs. You should be comfortable with Python classes and with the OpenAI-style chat API.
Most projects don't pick one approach and keep it forever. A prototype built with a framework may later need finer control. A hand-written SDK app may later need a retrieval pipeline you don't want to maintain. The safest way to move in either direction is in small steps, with the app working after each one.
Find the seam first
A seam is a place where you can swap one implementation for another without touching the rest of the code. Before migrating, put a small interface around every model call. Application code depends on the interface and never imports the SDK or the framework directly.
from typing import Protocol
from langchain_openai import ChatOpenAI
from openai import OpenAI
class TextModel(Protocol):
def complete(self, prompt: str) -> str: ...
class SdkModel:
def __init__(self, model: str = "gpt-4o-mini") -> None:
self._client = OpenAI()
self._model = model
def complete(self, prompt: str) -> str:
response = self._client.chat.completions.create(
model=self._model,
messages=[{"role": "user", "content": prompt}],
)
return response.choices[0].message.content or ""
class LangChainModel:
def __init__(self, model: str = "gpt-4o-mini") -> None:
self._llm = ChatOpenAI(model=model)
def complete(self, prompt: str) -> str:
return str(self._llm.invoke(prompt).content)
def summarize(model: TextModel, text: str) -> str:
return model.complete(f"Summarize in one sentence:\n{text}")
if __name__ == "__main__":
text = "Provider SDKs give direct control. Frameworks add reusable parts."
print(summarize(SdkModel(), text))
print(summarize(LangChainModel(), text))summarize doesn't know which implementation it receives. You can migrate one call site at a time, and you can run both implementations on the same input to compare results.
A migration order that works
Follow these steps in order:
- Wrap every model call behind an interface like
TextModel. - Save real prompts and replies as test cases. Model output varies, so check structure and key facts, not exact strings.
- Replace one component at a time. Start with the one that causes the most trouble.
- Check the exact request each version sends. Frameworks often add system text or reformat messages, so the same code can send a different prompt.
- Remove the old dependency only when nothing imports it.
Common directions
| Move | Typical reason | Hardest part |
|---|---|---|
| Framework to raw SDK | Debugging is opaque, or upgrades keep breaking code | Rewriting retries, streaming, and memory handling |
| Raw SDK to framework | Too much repeated boilerplate, or many providers to support | Learning the framework's abstractions |
| Raw SDK to PydanticAI | You hand-validate every reply | Rewriting tool definitions to its format |
Mixing approaches
Mixing is often better than a full switch. Each tool can do the job it is best at:
- Use LlamaIndex to load, chunk, and index documents, then pass the retrieved text to your own SDK call.
- Use PydanticAI for typed agents and keep plain SDK calls for simple one-shot prompts.
- Use LangChain's integrations for a vector store or document loader, without adopting its chain syntax.
The cost is more dependencies and more than one way to do the same thing. Write down which approach owns which part of the system, so the next developer does not guess.
Exercise
You are moving a LangChain app to the raw OpenAI SDK. Name the first step, and say why you should not replace everything at once.
Show answer
First, wrap every model call behind an interface such as TextModel and save sample inputs and outputs as tests. Replacing everything at once leaves you with many changes and no way to tell which one broke behavior. Doing one component at a time lets you compare the old and new outputs and roll back a single step.