Vision Models

Before calling, set your API key and TOKENGRID_BASE_URL environment variables. The base URL includes /v1. See the quick start.

Send images to vision-capable models using a supported chat interface.

Chat Completions API

Combine text with an image URL in message content. Choose a vision-capable Model ID and verify the interface supports the input format.

Python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["TOKENGRID_API_KEY"],
    base_url=os.environ["TOKENGRID_BASE_URL"]
)

response = client.chat.completions.create(
    model="YOUR_VISION_MODEL_ID",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "Describe this image."},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://example.com/image.jpg"
                    }
                }
            ]
        }
    ]
)

print(response.choices[0].message.content)

Using Base64 images

You can encode a local image as a Base64 data URL and include it in the request where supported. The examples use Python and JavaScript.

Python
import os
import base64
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["TOKENGRID_API_KEY"],
    base_url=os.environ["TOKENGRID_BASE_URL"]
)

def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode("utf-8")

image_base64 = encode_image("your_image.jpg")

response = client.chat.completions.create(
    model="YOUR_VISION_MODEL_ID",
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "Describe this image."},
            {
                "type": "image_url",
                "image_url": {
                    "url": f"data:image/jpeg;base64,{image_base64}"
                }
            }
        ]
    }]
)

print(response.choices[0].message.content)

Working with images

Image URL accessibility, expiration, size and format affect whether a model can read it.

Images requiring authentication

Fetch a private image in your application using appropriate authorization, then send an accepted representation. Do not expose private credentials in a URL.

Processing PDF and Word documents

If the interface does not directly accept a PDF or DOCX, preprocess it in your application. Convert PDF pages to images; for DOCX, extract text and optionally supported images. Process long documents in batches. The examples cover Python and JavaScript:

Python · PDF page conversion example
import os
import io
import base64
import pypdfium2 as pdfium
from openai import OpenAI

client = OpenAI(api_key=os.environ["TOKENGRID_API_KEY"],
                base_url=os.environ["TOKENGRID_BASE_URL"])

def pdf_to_images(path):
    document = pdfium.PdfDocument(path)
    parts = []
    try:
        for page in document:
            output = io.BytesIO()
            page.render(scale=2).to_pil().save(output, format="PNG")
            encoded = base64.b64encode(output.getvalue()).decode("utf-8")
            parts.append({"type": "image_url", "image_url": {
                "url": f"data:image/png;base64,{encoded}"}})
    finally:
        document.close()
    return parts

response = client.chat.completions.create(
    model="YOUR_VISION_MODEL_ID",
    messages=[{"role": "user", "content": [
        {"type": "text", "text": "Summarize this PDF."},
        *pdf_to_images("document.pdf")
    ]}]
)
print(response.choices[0].message.content)

Input limits

Check the selected model's image count, format and size limits, URL accessibility, download requirements and ordering of text and images. Limits vary by model and interface.