Skip to main content
Use Gemini’s native generateContent endpoint when your application sends contents and reads candidates. LaoZhang API uses the Gemini Developer API format with the v1beta version:
If your application already uses OpenAI-style messages, see the API integration guide for that client configuration.

Credentials and model selection

Create a model API key in Token management and set the LAOZHANG_API_KEY environment variable. These examples use gemini-3.8-flash. Check the model and your token group’s access in Models and pricing. Native Gemini requests accept either Authorization: Bearer or x-goog-api-key authentication. The Google Gen AI SDK supplies the API key header automatically.

Generate text

To use Google’s API key header instead, replace the Authorization header with:
The reply should contain connected. Do not append /v1 to the Google SDK base URL or use a Vertex AI project/location/publisher path for this endpoint.

Request structure

contents holds the messages in order. Each message has a role and one or more parts. REST JSON uses camelCase, such as inlineData and mimeType. Python SDK properties use names such as inline_data and mime_type; use the spelling appropriate to the interface you are calling. Optional settings and media support depend on the model.

Include an image

For a model that accepts image input, put a text part and an inlineData part in the same message. The following code reads a local input.png and writes the JSON request. Set LAOZHANG_GEMINI_MODEL to the image-capable model ID you selected in the console.
Send that file as the request body:
The MIME type must match the actual image. data contains raw Base64 without a data:image/png;base64, prefix. Describing an image and generating one are different operations. To create or edit images, use the model-specific instructions in Nano Banana 2 or Choose an image API.

Read the response

A text reply has this shape, with other metadata omitted:
Read text from the candidate’s content.parts, which may contain more than one part. The Python SDK’s response.text helper collects text for you. Keep the full candidate content when your application needs media, tool calls, or conversation history. If a response has no text, inspect promptFeedback, finishReason, and any non-text parts. Content blocking, an output limit, or a tool call needs different handling from a network error. Usage details, when present, appear in usageMetadata; account charges are available in call logs.

Continue a conversation

Send the earlier user message, the complete model reply, and the new user message in contents. Gemini uses model for model replies, not assistant. Preserve returned parts when continuing a model workflow. For models that return thoughtSignature, pass the signature back unchanged with its part. For tool-calling models, pair each functionCall with the appropriate functionResponse; a plain text summary is not a substitute for the tool result structure. See Google’s generateContent reference for these message formats.

Troubleshooting

  • 401: check the API key and authentication header. Use a model API key, not an account management AccessToken.
  • 404 or model not found: check v1beta, the exact model ID, and the token group’s native Gemini access.
  • 400: inspect contents, parts, message roles, media encoding, and optional configuration named in the error.
  • A response without text: inspect candidates, finish reasons, blocking feedback, and non-text content.
  • cURL works but the SDK fails: check the SDK’s base URL and API version. The final URL should contain /v1beta/models/ only once.
For a timeout or temporary server error, check call logs before retrying. Keep the request time, model, and redacted error when contacting support.