Skip to content

Add modern FastAPI integration example with dependency injection#2305

Open
Jacob-hash999 wants to merge 1 commit into
567-labs:mainfrom
Jacob-hash999:add-modern-fastapi-example
Open

Add modern FastAPI integration example with dependency injection#2305
Jacob-hash999 wants to merge 1 commit into
567-labs:mainfrom
Jacob-hash999:add-modern-fastapi-example

Conversation

@Jacob-hash999
Copy link
Copy Markdown

Add Modern FastAPI Integration Example (2026)

Summary

Updated FastAPI example to use current Instructor API (from_provider) instead of deprecated dsl module. Added dependency injection, async patterns, and proper error
handling.

Problem

The existing examples/fastapi_app/main.py uses the deprecated DSL API:

  • Uses dsl.ChatCompletion, ResponseSchema (no longer documented)
  • Not using current instructor.from_provider() pattern
  • Confusing for new users following the README

Solution

Created examples/modern-fastapi-integration/modern_fastapi_example.py showing 2026 best practices:

Before (old API):

task = (
    dsl.ChatCompletion(name="...")
    | dsl.SystemTask(task="...")
    | SearchRequest
)
return await task.acreate()

After (new API):
client = instructor.from_provider("openai/gpt-4o-mini")
result = client.chat.completions.create(
    response_model=List[SearchQuery],
    messages=[...],
)
return SearchResponse(queries=result)

Features

-Modern API: Uses instructor.from_provider()
-Dependency Injection: FastAPI best practice for sharing clients
-Async: Proper async/await patterns
-Error Handling: HTTPException with informative messages
-Type Safety: Full type hints for IDE support
-Production Ready: Includes health check endpoint

Testing

python examples/modern-fastapi-integration/modern_fastapi_example.py

# Then test:
curl -X POST http://localhost:8000/search \
     -H "Content-Type: application/json" \
     -d '{"text": "Find ML engineers with Python experience"}'

Why This Matters

I use Instructor daily in production and the DSL example was confusing when I started. This example follows the README patterns and will help new users get started faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant