Gemini API (Google)
Use the Gemini-native API at /v1beta/models/{model}:generateContent with the google-genai SDK. Text, images, tools, structured output, streaming, and current limits.
Use this page with an AI assistant
Opens a new chat with this docs URL and the correct AICredits base URLs.
AICredits accepts requests in the Gemini API format, so you can point the google-genai SDK at AICredits and keep your existing code.
| Method | Path | Purpose |
|---|---|---|
POST | /v1beta/models/{model}:generateContent | Single response |
POST | /v1beta/models/{model}:streamGenerateContent | Streaming response |
Setup
Use the bare base URL https://api.aicredits.in (without /v1 or /v1beta). The SDK adds /v1beta/models/... itself.
from google import genai
from google.genai import types
client = genai.Client(
api_key="sk-your-key-here", # your AICredits API key
http_options=types.HttpOptions(base_url="https://api.aicredits.in"),
)
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="Explain UPI in one sentence.",
)
print(response.text)curl "https://api.aicredits.in/v1beta/models/gemini-2.5-flash:generateContent" \
-H "x-goog-api-key: sk-your-key-here" \
-H "Content-Type: application/json" \
-d '{
"contents": [{"role": "user", "parts": [{"text": "Explain UPI in one sentence."}]}]
}'Authentication
Send your AICredits API key in any one of these ways:
x-goog-api-key: sk-...header (what the google-genai SDK sends)?key=sk-...query parameter (accepted on/v1beta/routes only)Authorization: Bearer sk-...header
Which models work
The {model} in the path can be any model on AICredits, not only Gemini models. The request is converted to chat format internally, routed and billed like any other request, and the answer is converted back to Gemini format. Model IDs containing a slash, such as openai/gpt-4o, work as written in the path.
Streaming
:streamGenerateContent has two output modes:
- With
?alt=sse, you receive server-sent events, each adata:line holding a Gemini response chunk. The google-genai SDK uses this mode. - Without it, the response is a single JSON array of chunks.
curl -N "https://api.aicredits.in/v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse" \
-H "x-goog-api-key: sk-your-key-here" \
-H "Content-Type: application/json" \
-d '{"contents": [{"role": "user", "parts": [{"text": "Tell me a short story."}]}]}'Text arrives chunk by chunk. The closing chunk carries finishReason and usageMetadata.
Tool calls in streams
Function calls are not streamed piece by piece. They arrive whole, as functionCall parts in the closing chunk.
Supported features
- Text and system prompts:
contentswithuserandmodelroles, andsystemInstruction. - Images:
inlineDataparts with animage/*MIME type and base64 data. generationConfig:temperature,topP,topK,maxOutputTokens,stopSequences,responseMimeType,responseSchema, andresponseJsonSchema.- Structured output:
responseSchemaorresponseJsonSchemais enforced as a JSON schema.responseMimeType: "application/json"on its own requests plain JSON output. - Function calling:
tools[].functionDeclarations(withparametersorparametersJsonSchema), andtoolConfig.functionCallingConfigwith modeAUTO,NONE, orANY. WithANYand exactly one name inallowedFunctionNames, that function is forced. WithANYand any other number of names, the model is required to call a tool. - Tool round trips:
functionCallparts from the model andfunctionResponseparts in your next user turn.
from google.genai import types
get_weather = types.FunctionDeclaration(
name="get_weather",
description="Get the weather for a city",
parameters={
"type": "OBJECT",
"properties": {"city": {"type": "STRING"}},
"required": ["city"],
},
)
response = client.models.generate_content(
model="anthropic/claude-sonnet-4.6",
contents="Weather in Mumbai?",
config=types.GenerateContentConfig(tools=[types.Tool(function_declarations=[get_weather])]),
)
print(response.function_calls)Response
Responses use Gemini's shape: candidates with content.parts, a finishReason, usageMetadata (promptTokenCount, candidatesTokenCount, totalTokenCount), modelVersion, and responseId. finishReason is MAX_TOKENS when the output hit the length limit, SAFETY when a content filter stopped it, and STOP otherwise.
Errors
Errors on these routes use Gemini's envelope:
{
"error": {
"code": 400,
"message": "Invalid request format: ...",
"status": "INVALID_ARGUMENT"
}
}Common statuses: 400 INVALID_ARGUMENT, 401 UNAUTHENTICATED, 402 FAILED_PRECONDITION, 403 PERMISSION_DENIED, 404 NOT_FOUND, 429 RESOURCE_EXHAUSTED, 501 UNIMPLEMENTED, 503 UNAVAILABLE, 504 DEADLINE_EXCEEDED. Other 5xx errors return INTERNAL.
Limitations
:countTokensis not supported. It returns501 UNIMPLEMENTED. Read token usage fromusageMetadataon each response instead.topKonly reaches Anthropic-routed models. For other models it is accepted and ignored.- Images only for
inlineData. Audio, video, PDF, and other non-image MIME types return400. - Ignored fields: thought parts,
candidateCountabove 1 (you get one candidate), and Gemini built-in tools such asgoogleSearchandcodeExecution. OnlyfunctionDeclarationsare used fromtools. - Request body: 10 MB maximum.
For the broadest provider compatibility, Chat Completions is the most fully featured surface.