Back to Repositories

Testing Chat Completion API Endpoints in private-gpt

This test suite evaluates the chat completion routes in the private-gpt server implementation. It focuses on validating both streaming and single-value response patterns from the chat API endpoints, ensuring proper handling of OpenAI-compatible message formats.

Test Coverage Overview

The test suite provides essential coverage for the chat completion functionality, testing both streaming and non-streaming response modes.

  • Validates streaming response format and event structure
  • Tests single-value completion responses
  • Verifies proper content-type headers
  • Ensures OpenAI-compatible message validation

Implementation Analysis

The testing approach utilizes FastAPI’s TestClient to simulate HTTP requests to the chat completion endpoints. It implements two distinct test cases that verify different response patterns, using the ChatBody model for request structuring and OpenAICompletion for response validation.

The tests leverage FastAPI’s model validation capabilities and event stream parsing for comprehensive endpoint verification.

Technical Details

Testing tools and configuration:

  • FastAPI TestClient for HTTP request simulation
  • OpenAI model schemas for request/response validation
  • ChatBody data model for request structuring
  • Event stream parsing for SSE validation
  • Pytest fixtures for client setup

Best Practices Demonstrated

The test suite exemplifies several testing best practices for API validation. It includes isolated test cases for different response types, proper model validation, and explicit assertion checks.

  • Separate concerns for streaming vs non-streaming tests
  • Schema validation for response integrity
  • Clear test case organization
  • Explicit status code verification

zylon-ai/private-gpt

tests/server/chat/test_chat_routes.py

            
from fastapi.testclient import TestClient

from private_gpt.open_ai.openai_models import OpenAICompletion, OpenAIMessage
from private_gpt.server.chat.chat_router import ChatBody


def test_chat_route_produces_a_stream(test_client: TestClient) -> None:
    body = ChatBody(
        messages=[OpenAIMessage(content="test", role="user")],
        use_context=False,
        stream=True,
    )
    response = test_client.post("/v1/chat/completions", json=body.model_dump())

    raw_events = response.text.split("

")
    events = [
        item.removeprefix("data: ") for item in raw_events if item.startswith("data: ")
    ]
    assert response.status_code == 200
    assert "text/event-stream" in response.headers["content-type"]
    assert len(events) > 0
    assert events[-1] == "[DONE]"


def test_chat_route_produces_a_single_value(test_client: TestClient) -> None:
    body = ChatBody(
        messages=[OpenAIMessage(content="test", role="user")],
        use_context=False,
        stream=False,
    )
    response = test_client.post("/v1/chat/completions", json=body.model_dump())

    # No asserts, if it validates it's good
    OpenAICompletion.model_validate(response.json())
    assert response.status_code == 200