Fine-tuning GPT-4o-mini for SQL generation - results and tips

Sarah Chen
Sarah ChenMar 22, 2026

I fine-tuned GPT-4o-mini to generate SQL queries for our specific database schema and the results are impressive. Sharing my approach.

Dataset

  • 3,200 (question, SQL) pairs
  • Covered 15 tables, various JOIN types, aggregations
  • Included edge cases and complex subqueries
  • Results

  • Base GPT-4o-mini: 61% correct SQL on our test set
  • Fine-tuned: 89% correct SQL
  • GPT-4o (no fine-tuning): 79% correct SQL
  • So the fine-tuned mini model outperforms the base GPT-4o at a fraction of the cost!

    Key tips

    1. Include the schema in every training example's system prompt 2. Add error correction examples (wrong SQL -> correct SQL) 3. Use 2 epochs max — overfitting happens fast 4. Include diverse question phrasings for the same query

    6.1k views33 replies88 likes
    2 Replies
    Karen Wu
    Karen WuStaffAccepted AnswerMay 23

    This is likely related to your network layer, not the API itself. Make sure you're properly handling the SSE stream. Some common causes:

    1. Proxy or load balancer buffering chunks 2. Missing Connection: keep-alive header 3. Client-side buffer overflow

    Try using the SDK's built-in streaming which handles these edge cases:

    with client.chat.completions.create(
        model="gpt-4o",
        messages=[...],
        stream=True
    ) as stream:
        for text in stream:
            # This handles all edge cases
            pass
    
    Nina Patel

    It was our nginx proxy buffering! Added proxy_buffering off; and the issue disappeared. Thanks!

    Log in to reply to this topic.