Gemini native protocol access

Use contents and parts to invoke native text and multimodal generation.

Initiate a request

Using a model that supports Gemini’s native protocol, write the real model ID to the URL:

curl --fail-with-body "https://dunduntoken.com/v1beta/models/$DUNDUN_MODEL:generateContent" \
  -H "x-goog-api-key: $DUNDUN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"contents":[{"role":"user","parts":[{"text":"只回复 OK"}]}]}'

Parse content

Find text or media in candidates[].content.parts[]. When there are no candidates, check the safety feedback, end reason and error object, and do not directly access the first element that does not exist.

Streaming and Pictures

The streaming method is :streamGenerateContent; clients using SSE should configure the response format as required by the interface. The image model may require generationConfig to specify the output type and return the media in inlineData, which cannot be forced to be parsed according to Chat’s choices.

Addresses and Boundaries

The client base address should cooperate with its automatic splicing rules so that /v1beta appears only once in the final URL. :countTokens is currently not implemented, so do not use it as a pre-access health check. Veo long tasks are handled by the corresponding plug-in, see Video Task.

Basic response instructions

{
  "candidates": [
    {
      "content": {
        "role": "model",
        "parts": [
          {
            "text": "OK"
          }
        ]
      },
      "finishReason": "STOP"
    }
  ]
}

Iterate through all parts and extract content by type. Check the response feedback when the candidate is empty; when finishReason is not completed normally, the text is not directly used as the completed structured result.

Multiple rounds of dialogue

{
  "contents": [
    {
      "role": "user",
      "parts": [
        {
          "text": "只回复 OK"
        }
      ]
    },
    {
      "role": "model",
      "parts": [
        {
          "text": "OK"
        }
      ]
    },
    {
      "role": "user",
      "parts": [
        {
          "text": "解释刚才的回答"
        }
      ]
    }
  ]
}

The native model character is model. System commands and tools use Gemini’s native structure and do not apply OpenAI’s system messages and function schema packaging.

Streaming command

curl -N --fail-with-body "https://dunduntoken.com/v1beta/models/$DUNDUN_MODEL:streamGenerateContent?alt=sse"   -H "x-goog-api-key: $DUNDUN_API_KEY"   -H "Content-Type: application/json"   -d '{"contents":[{"role":"user","parts":[{"text":"只回复 OK"}]}]}'

DUNDUN_MODEL needs to be set to the currently available model. Use request headers to pass keys to avoid writing Keys into URL queries and access logs.

Error location

404 checks version, model and method in path; 400 checks contents/parts structure and generationConfig; media failure checks MIME and Base64. If the client connection test fixedly calls the unimplemented countTokens, use the real minimum generation verification instead, and do not use the detection results alone to determine that the model is unavailable.