How to beginner · 3 min read

How to add AI autocomplete to text editor

Quick answer
Add AI autocomplete to a text editor by sending the current text context to a language model like gpt-4o via the OpenAI API and appending the model's completion as suggestions. Use the chat.completions.create method with the user's partial input to generate relevant autocomplete options.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the OpenAI Python SDK and set your API key as an environment variable to securely authenticate requests.

bash
pip install openai>=1.0

Step by step

This example shows how to send the current text from a text editor to the gpt-4o model to get autocomplete suggestions. The code reads partial input and returns a completion to append.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

def get_autocomplete_suggestion(partial_text):
    prompt = f"Complete this text: {partial_text}"
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": prompt}],
        max_tokens=20,
        temperature=0.7
    )
    completion = response.choices[0].message.content.strip()
    return completion

# Example usage
if __name__ == "__main__":
    user_input = "The quick brown fox"
    suggestion = get_autocomplete_suggestion(user_input)
    print(f"Autocomplete suggestion: {suggestion}")
output
Autocomplete suggestion: jumps over the lazy dog.

Common variations

You can implement async calls for better UI responsiveness, use streaming completions to show suggestions as they generate, or switch to other models like claude-3-5-sonnet-20241022 for different autocomplete styles.

python
import os
import asyncio
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

async def get_autocomplete_async(partial_text):
    prompt = f"Complete this text: {partial_text}"
    response = await client.chat.completions.acreate(
        model="gpt-4o",
        messages=[{"role": "user", "content": prompt}],
        max_tokens=20,
        temperature=0.7
    )
    return response.choices[0].message.content.strip()

# Async example usage
async def main():
    user_input = "Artificial intelligence is"
    suggestion = await get_autocomplete_async(user_input)
    print(f"Async autocomplete suggestion: {suggestion}")

if __name__ == "__main__":
    asyncio.run(main())
output
Async autocomplete suggestion: transforming industries worldwide.

Troubleshooting

  • If you get authentication errors, verify your OPENAI_API_KEY environment variable is set correctly.
  • If completions are irrelevant, adjust temperature or provide more context in the prompt.
  • For rate limit errors, implement exponential backoff retries or upgrade your API plan.

Key Takeaways

  • Use the gpt-4o model with the OpenAI Python SDK for effective autocomplete.
  • Send the current partial text as a prompt to generate relevant completions.
  • Async and streaming completions improve user experience in interactive editors.
  • Adjust model parameters like temperature to control suggestion creativity.
  • Always secure your API key using environment variables to avoid leaks.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗