Chat

Send a conversation, get a reply. Chat powers assistants, summaries, classification, code generation and most other things you'd use a language model for.

Send a message

Every request has a model and a list of messages. Each message has a role and some content:

  • system gives the model its instructions.
  • user is the prompt.
  • assistant is one of the model's earlier replies.

Pendra doesn't remember earlier turns, so send the whole conversation each time.

from pendra import Pendra

client = Pendra(api_key="pdr_sk_...")

response = client.chat.completions.create(
    model="qwen3.6:27b",
    messages=[
        {"role": "system", "content": "You are a concise assistant."},
        {"role": "user", "content": "Explain GPUs in one sentence."},
    ],
)
print(response.choices[0].message.content)
Response

A GPU is a specialised processor built for the massively parallel arithmetic that graphics and, more recently, AI workloads depend on.

System messages

Put your instructions in a system message at the start. You can add one later in the conversation too, and Pendra places it where the model will read it. A developer message works the same way.

If a model can't accept a conversation, for example because a system message sits between two user messages, you get a 400 that says why. Move the instruction to the start, or join the two user messages.

Stream the reply

Set stream to true to show the reply as it's written, rather than waiting for all of it. It's the best choice for anything interactive. Requests that don't stream are fine too, even for replies that take up to about 30 minutes.

stream = client.chat.completions.create(
    model="qwen3.6:27b",
    stream=True,
    messages=[{"role": "user", "content": "Write a haiku."}],
)
for event in stream:
    if not event.choices:  # usage chunks carry no text
        continue
    print(event.choices[0].delta.content or "", end="")

Shape the output

Lower the temperature for focused, factual answers, or raise it for more variety. Use max_tokens to cap the length and stop to end the reply at a string you choose. The other usual settings work too, including top_p, top_k, seed and the repetition penalties.

Anything you don't set uses the model's recommended settings, which you can see in the models list. Those can change, so set them yourself when you need results that stay the same, for example in tests.

# Pin the sampling settings so results don't shift if the
# model's recommended defaults change
response = client.chat.completions.create(
    model="qwen3.6:27b",
    temperature=0.2,
    top_p=0.9,
    seed=42,
    messages=[{"role": "user", "content": "Summarise this ticket in one line."}],
)

Attach a file

Add a file part to a message to have the model read a .txt or .csv file along with your question. Send the file as base64 in file_data, up to 1 MB per file. Nothing is stored, so the file only exists for that request.

import base64

with open("sales.csv", "rb") as f:
    data = base64.b64encode(f.read()).decode()

response = client.chat.completions.create(
    model="qwen3.6:27b",
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "Which region had the highest revenue?"},
            {"type": "file", "file": {
                "filename": "sales.csv",
                "file_data": f"data:text/csv;base64,{data}",
            }},
        ],
    }],
)

The file counts towards the prompt, and 1 MB is about a quarter of a million tokens, so a large file needs a big enough context window. If you attach several, refer to each one by name in your question. Other file types return a 400. To send images, see Vision.

Thinking

Reasoning models work through a problem before they answer, which helps with maths, logic, planning and tricky code. You don't need to turn it on. The working comes back in reasoning_content, separate from the answer in content, and streams as delta.reasoning_content. Models that reason have thinking in their capabilities.

response = client.chat.completions.create(
    model="qwen3.6:27b",
    reasoning_effort="low",
    messages=[{"role": "user", "content": "Plan a three-day trip to Edinburgh."}],
)

msg = response.choices[0].message
print(msg.reasoning_content)  # the model's working
print(msg.content)            # the answer

Turn it down or off

  • reasoning_effort sets how hard the model thinks, from "low" to "max". If a model doesn't have the level you ask for, it uses the next one up. Each model's levels are in the models list.
  • enable_thinking: false gets a direct answer, which is quicker for simple jobs like classification. A model that always thinks will think as little as it can instead.
  • A few small models only think when you ask, with enable_thinking: true. Their default_thinking is false.

Thinking budget

So a model can't think forever, Pendra stops the thinking after 8,192 tokens (2,048 on "low", 24,576 on "high" and above) and the model moves on to its answer. Set your own budget with reasoning: { max_tokens: 4096 }. The budget comes on top of max_tokens, which is kept for the answer. A reply with no max_tokens stops at 32,768 tokens, thinking included.

A few models, including gpt-oss and Gemma 4, can't stop part-way through their thinking, so they get no default budget. On these, a budget you set ends the reply before it answers. Their reasoning_controls.budget is false.

Longer conversations

When you send the conversation back, keep each assistant message's reasoning_content as you received it. Some models use it to stay on plan across tool calls. It counts towards the prompt, so drop it from older turns if a long conversation nears the context window.

Tool calling

Give the model functions it can call, such as a search, your own API or a database query. Describe each one with a name, description and JSON Schema parameters. When the model wants one, it replies with tool_calls instead of an answer.

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get the current weather for a city",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    },
}]

response = client.chat.completions.create(
    model="qwen3.6:27b",
    tools=tools,
    messages=[{"role": "user", "content": "What's the weather in Cardiff?"}],
)

call = response.choices[0].message.tool_calls[0]
print(call.function.name, call.function.arguments)
Tool call
get_weather {"city": "Cardiff"}

You run the tool and send the result back:

  1. Add the assistant message to the conversation as you received it.
  2. Add a tool message for each call, with its tool_call_id and the result as content.
  3. Send the conversation again, and the model uses the results to answer.

tool_choice controls whether the model calls a tool. Leave it as "auto" to let the model decide, set "required" to force a call or "none" to stop one, or name a function to call exactly that one. Reasoning models think before they call, so give them enough max_tokens.

When you stream, a call's arguments arrive in pieces that share an index. Join them before you parse, and if finish_reason is "length", the last call is incomplete.

Structured outputs

Set response_format to get JSON back instead of prose. { "type": "json_object" } returns any valid JSON. A json_schema returns JSON that matches your schema, and "strict": true makes it match exactly, so you can load it straight into your own types.

response = client.chat.completions.create(
    model="qwen3.6:27b",
    messages=[{"role": "user", "content": "Extract the name and age from: Sara is 34."}],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "person",
            "strict": True,
            "schema": {
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "age": {"type": "integer"},
                },
                "required": ["name", "age"],
                "additionalProperties": False,
            },
        },
    },
)
print(response.choices[0].message.content)
Response
{ "name": "Sara", "age": 34 }

Pendra adds your schema to the system message, so you don't need to describe the format in your prompt. Keep response_format the same on every turn, because changing it makes the model read the whole conversation again.

You can send tools and response_format together. Each reply is then either tool calls or an answer that matches your schema.

If the JSON might not be what you asked for, the reply carries a pendra.notice:

  • truncated_during_structured_output means the reply hit max_tokens before the JSON was finished. Raise the limit.
  • structured_output_not_json on a reasoning model means it never got past thinking to its answer. Lower reasoning_effort or set a thinking budget.
  • The same notice on any other model means the schema couldn't be applied, usually because it's very large or deeply nested. Simplify it.
  • strict_schema_not_enforced means part of your schema, such as anyOf or pattern, can't be enforced. Check those fields yourself.
For every field and response, see the Chat completions API reference.