Text generation
Generate and structure text with language models
NaviGator AI Toolkit exposes an OpenAI-compatible Chat Completions API for drafting, summarizing, transforming, classifying and extracting information from text.
Quick start
Before running an example, complete Getting Started, configure the documented environment variables and confirm that your key can use gpt-oss-120b. The Python and JavaScript examples also require the openai package from OpenAI-compatible SDKs.
This example uses the active, locally hosted gpt-oss-120b model.
curl "$NAVIGATOR_API_BASE_URL/chat/completions" \
--header "Authorization: Bearer $NAVIGATOR_TOOLKIT_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "gpt-oss-120b",
"messages": [
{"role": "system", "content": "You are a concise, helpful assistant."},
{"role": "user", "content": "Explain retrieval-augmented generation in three sentences."}
]
}'A successful request returns a three-sentence explanation. The curl example prints the full JSON response, with the text under choices[0].message.content; the other examples print only the text. The exact wording varies.
See OpenAI-compatible SDKs for client installation and shared configuration.
Build an effective prompt
A useful prompt typically includes:
- Instructions: State the task directly.
- Context: Supply the information needed to complete it.
- Constraints: Specify length, tone, audience or exclusions.
- Output format: Describe the desired structure and provide an example when useful.
Separate stable application behavior into a system message and the current request into a user message. Models can still produce inaccurate output, so verify important results.
Continue a conversation
Chat-completion requests are stateless. To preserve context, include the relevant earlier user and assistant messages in each new request. Every included message counts toward the model's context window and usage.
Choose a model
Consider:
- Input and output modalities
- Context and output limits
- Task specialization, such as coding or reasoning
- Latency and pricing
- Deployment location
- Approved data classifications
- Active, Legacy or End of Life (EOL) status
Use the model inventory to compare current NaviGator AI Toolkit models. Larger models are not automatically the best choice for every task; test representative prompts against your quality, latency and cost requirements.
Structured output
Some models support JSON mode or schema-constrained output. Support varies by model and provider—do not assume every chat model accepts the same response_format options.
For reliable application behavior:
- Validate returned JSON before using it.
- Handle refusals, incomplete output and schema-validation failures.
- Test the exact model and deployment used in production.
There are two kinds of structured output:
- JSON mode: The model returns valid JSON, but its structure can change between requests.
- JSON schema: You supply a schema, and the model returns output in that format.
JSON mode
Set response_format to {"type": "json_object"} and ask for JSON in the prompt:
curl "$NAVIGATOR_API_BASE_URL/chat/completions" \
--header "Authorization: Bearer $NAVIGATOR_TOOLKIT_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "gpt-oss-120b",
"response_format": {"type": "json_object"},
"messages": [
{"role": "system", "content": "Return the event as JSON with name, date and participants."},
{"role": "user", "content": "Alice and Bob are going to a science fair on Friday."}
]
}'JSON schema with Pydantic
Install Pydantic with the OpenAI client:
python -m pip install openai pydanticThis example uses the locally hosted gpt-oss-20b model:
import os
from openai import OpenAI
from pydantic import BaseModel
class CalendarEvent(BaseModel):
name: str
date: str
participants: list[str]
client = OpenAI(
api_key=os.environ["NAVIGATOR_TOOLKIT_API_KEY"],
base_url=os.environ.get(
"NAVIGATOR_API_BASE_URL",
"https://api.navigator.ai.ufl.edu/v1",
),
)
response = client.chat.completions.parse(
model="gpt-oss-20b",
messages=[
{"role": "system", "content": "Extract event information."},
{
"role": "user",
"content": "Alice and Bob are going to a science fair on Friday.",
},
],
response_format=CalendarEvent,
)
print(response.choices[0].message)The returned message includes the parsed CalendarEvent under parsed, or a refusal when the model declines.
chat.completions.parse needs a recent openai package. If Python reports that Completions has no attribute parse, upgrade with python -m pip install --upgrade openai.
The Responses API guide includes a structured-output example using Pydantic.
Stream the response
Streaming returns the answer in pieces as it is generated, so people see text sooner in an interactive application. Set stream to true.
curl --no-buffer "$NAVIGATOR_API_BASE_URL/chat/completions" \
--header "Authorization: Bearer $NAVIGATOR_TOOLKIT_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "gpt-oss-120b",
"stream": true,
"messages": [{"role": "user", "content": "Write a haiku about alligators."}]
}'Each line beginning with data: holds one piece of the answer. The stream ends with data: [DONE].
If a stream stops early, see Errors and retries.
Common problems
The request returns 401 Unauthorized
- Likely cause: The key is missing, expired, revoked or not sent as a bearer token.
- Check: Confirm the credential variable exists without printing its value, then inspect the request's
Authorizationheader name and scheme. - Fix: Load a valid key through the approved credential method and send it as
Authorization: Bearer <key>.
The model is unavailable
- Symptom: The request returns a model-not-found or access error.
- Likely cause: The model ID is incorrect or is not assigned to the key's team.
- Check: List the models available to the key and compare the exact, case-sensitive ID.
- Fix: Use an assigned model ID or ask a Team Admin to review model access.
The input exceeds the context limit
- Symptom: The request returns a context-length error.
- Likely cause: The prompt and included conversation are larger than the selected model's context window.
- Check: Compare the request size and conversation history with the model page's context limit.
- Fix: Reduce the input or history, or select an approved model with a larger context window.
The response has an unexpected format
- Symptom: The response is valid text but does not follow the requested structure.
- Likely cause: The prompt is ambiguous or the selected model does not support the requested structured-output option.
- Check: Review the prompt's format instructions and the selected model's documented capabilities.
- Fix: Make the format explicit and use structured output only when the selected model supports it.