Skip to content

Conversation

@KennyVaneetvelde
Copy link
Member

This adds a simple FastAPI example to help developers get started with integrating Atomic Agents into their web APIs.

What's included

  • Basic FastAPI app with session-based conversation management
  • RESTful endpoints for chat, session listing, and cleanup
  • Environment configuration setup
  • Comprehensive README with usage examples

Why this approach

While we have the MCP-based FastAPI example which shows advanced usage, this provides a more approachable starting point for developers who just want to add conversational AI to their FastAPI apps.

The example uses the empty fastapi-memory/ directory that was already in the repo.

Closes #40

🤖 Generated with Claude Code

Adds a straightforward example showing how to use Atomic Agents with FastAPI.
Includes session management, RESTful endpoints, and proper cleanup patterns.

Resolves #40

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@greptile-apps
Copy link

greptile-apps bot commented Nov 21, 2025

Automated review by Greptile

Greptile Overview

Greptile Summary

This PR successfully adds a well-structured FastAPI integration example demonstrating multi-user, multi-session conversational AI with Atomic Agents. The implementation addresses issue #40 by providing an approachable starting point for developers.

Key improvements from previous review iterations:

  • ✅ Fixed AtomicAgent type parameters - properly instantiated as AtomicAgent[ChatRequest, ChatResponse]
  • ✅ Removed hardcoded Windows path from config.py
  • ✅ Corrected agent instantiation to use factory functions with proper schemas
  • ✅ All imports from atomic_agents follow v2.0+ conventions (no deprecated .lib paths)

What this PR delivers:

  • Comprehensive session management with UUID-based session IDs
  • Conversation history persistence across sessions
  • Both streaming and non-streaming chat endpoints
  • RESTful API design with proper Pydantic validation
  • Feature-rich CLI client with Rich terminal UI
  • Complete test coverage via test_api.py
  • Detailed documentation with usage examples

Minor documentation issue:

  • README references .env.example file in project structure (line 214) but the file doesn't exist in the PR

The code follows Atomic Agents best practices from agents.md, properly using BaseIOSchema for type-safe contracts, SystemPromptGenerator for behavior definition, and correct agent instantiation patterns. The example effectively demonstrates production-ready patterns for building multi-tenant conversational APIs.

Confidence Score: 4/5

  • This PR is safe to merge with only a minor documentation discrepancy
  • Score reflects solid implementation that addresses all previously identified critical issues (type parameters, schema mismatches, hardcoded paths). The code follows framework best practices, includes comprehensive error handling, and provides excellent documentation. Only deduction is for the missing .env.example file referenced in README, which is a minor documentation inconsistency that doesn't affect functionality
  • No files require special attention - all previously flagged issues have been resolved

Important Files Changed

File Analysis

Filename Score Overview
atomic-examples/fastapi-memory/fastapi_memory/main.py 4/5 Core FastAPI server with session management, properly typed agents, good error handling
atomic-examples/fastapi-memory/fastapi_memory/lib/agents/chat_agent.py 5/5 Well-structured agent factory with proper type parameters and clear documentation
atomic-examples/fastapi-memory/fastapi_memory/lib/schemas.py 5/5 Clean Pydantic schemas extending BaseIOSchema correctly with comprehensive documentation
atomic-examples/fastapi-memory/fastapi_memory/lib/config.py 5/5 Configuration with cross-platform .env fallback logic, hardcoded Windows path removed
atomic-examples/fastapi-memory/README.md 4/5 Comprehensive documentation with setup instructions, references non-existent .env.example file

Sequence Diagram

sequenceDiagram
    participant Client
    participant FastAPI
    participant SessionManager
    participant AtomicAgent
    participant OpenAI

    Note over Client,FastAPI: Session Creation Flow
    Client->>FastAPI: POST /users/{user_id}/sessions
    FastAPI->>SessionManager: _generate_session_id()
    SessionManager-->>FastAPI: session_id (UUID)
    FastAPI->>SessionManager: Store in session_metadata
    FastAPI-->>Client: SessionCreateResponse

    Note over Client,OpenAI: Chat Flow (Non-Streaming)
    Client->>FastAPI: POST /chat (ChatRequest)
    FastAPI->>SessionManager: _add_message_to_history("user", message)
    FastAPI->>SessionManager: get_or_create_agent(user_id, session_id)
    SessionManager->>AtomicAgent: create_chat_agent() if new
    SessionManager-->>FastAPI: agent instance
    FastAPI->>AtomicAgent: agent.run(ChatRequest)
    AtomicAgent->>OpenAI: API call with system prompt + history
    OpenAI-->>AtomicAgent: ChatResponse
    AtomicAgent-->>FastAPI: ChatResponse
    FastAPI->>SessionManager: _add_message_to_history("assistant", response)
    FastAPI-->>Client: ChatResponse

    Note over Client,OpenAI: Chat Flow (Streaming)
    Client->>FastAPI: POST /chat/stream (ChatRequest)
    FastAPI->>SessionManager: _add_message_to_history("user", message)
    FastAPI->>SessionManager: get_or_create_async_agent(user_id, session_id)
    SessionManager->>AtomicAgent: create_async_chat_agent() if new
    SessionManager-->>FastAPI: async agent instance
    FastAPI->>AtomicAgent: agent.run_async_stream(ChatRequest)
    loop Stream chunks
        AtomicAgent->>OpenAI: Streaming API call
        OpenAI-->>AtomicAgent: Response chunk
        AtomicAgent-->>FastAPI: ChatResponse chunk
        FastAPI-->>Client: SSE data event
    end
    FastAPI->>SessionManager: _add_message_to_history("assistant", full_response)

    Note over Client,FastAPI: History & Session Management
    Client->>FastAPI: GET /users/{user_id}/sessions/{session_id}/history
    FastAPI->>SessionManager: Fetch from conversation_history
    FastAPI-->>Client: ConversationHistory

    Client->>FastAPI: DELETE /users/{user_id}/sessions/{session_id}
    FastAPI->>SessionManager: Remove from sessions/async_sessions/metadata/history
    FastAPI-->>Client: SessionDeleteResponse
Loading

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, 3 comments

Edit Code Review Agent Settings | Greptile

Updates imports to use the correct paths and adds docstrings to schema
classes as required by BaseIOSchema.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

- Add proper type parameters to AtomicAgent instantiation
- Update README to match other examples' setup pattern
- Remove .env.example (gitignored pattern)
- Verified end-to-end functionality with actual API calls

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

22 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

22 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@KennyVaneetvelde KennyVaneetvelde merged commit 6e57272 into main Nov 23, 2025
5 checks passed
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.

FastAPI usage example

2 participants