> ## Documentation Index
> Fetch the complete documentation index at: https://docs.znapai.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Text Generation

> Learn how to use the Vercel AI SDK for text generation, streaming responses, and handling parameters or errors.

The Vercel AI SDK provides powerful functions for text generation and streaming. Below are details and examples for implementing these functions.

***

## Text Generation

Use `generateText()` for standard, non-streaming responses.

```typescript theme={null}
import { generateText } from 'ai';
import { openai, MODEL } from './client';

async function generateGreeting() {
  const response = await generateText({
    model: openai(MODEL),
    prompt: 'Explain quantum computing in one sentence.',
  });

  console.log(response.text);
}
```

***

## Streaming Responses

Use `streamText()` to stream tokens as they are generated.

```typescript theme={null}
import { streamText } from 'ai';
import { openai, MODEL } from './client';

async function streamResponse() {
  const result = await streamText({
    model: openai(MODEL),
    prompt: 'Write a poem about a software engineer.',
  });

  for await (const chunk of result.textStream) {
    process.stdout.write(chunk);
  }
}
```

Streaming improves perceived latency and is recommended for chat interfaces.

***

## System Prompts & Parameters

You can customize model behavior using a system prompt and generation parameters.

### Supported Parameters

<ParamField path="model" type="LanguageModel" required>
  The model instance to use for generation.
</ParamField>

<ParamField path="prompt" type="string">
  The text prompt to generate a response for. Either `prompt` or `messages` is required.
</ParamField>

<ParamField path="system" type="string">
  System instructions to guide the model's behavior.
</ParamField>

<ParamField path="temperature" type="number">
  Controls randomness. Lower values produce more deterministic responses.
</ParamField>

<ParamField path="maxTokens" type="number">
  Maximum number of tokens generated.
</ParamField>

<ParamField path="topP" type="number">
  Nucleus sampling probability.
</ParamField>

<ParamField path="topK" type="number">
  Limits sampling to the top K probable tokens.
</ParamField>

<ParamField path="presencePenalty" type="number">
  Encourages introducing new topics.
</ParamField>

<ParamField path="frequencyPenalty" type="number">
  Reduces repeated words or phrases.
</ParamField>

<ParamField path="seed" type="number">
  Produces deterministic outputs when supported.
</ParamField>

<ParamField path="stopSequences" type="string[]">
  Stops generation when one of the specified sequences is encountered.
</ParamField>

## Error Handling

Wrap SDK calls in a `try...catch` block to handle API errors, validation failures, and authentication issues.

```typescript theme={null}
import { generateText } from 'ai';
import { openai, MODEL } from './client';

async function safeGenerate() {
  try {
    const response = await generateText({
      model: openai(MODEL),
      prompt: 'Hello!',
      temperature: -1,
    });

    console.log(response.text);
  } catch (error: any) {
    console.error('Error:', error.name);
    console.error(error.message);
  }
}
```

Common errors include:

* Invalid API key
* Incorrect endpoint URL
* Unsupported model
* Invalid request parameters
* Rate limits
