Skip to content

Gemini Function Calling does not respect response schema field ordering #1879

@raghav-malik-bst

Description

@raghav-malik-bst

Summary

When using Gemini Function Calling, function-call arguments are emitted in an order that does not match the schema definition, even when the schema clearly specifies a fixed property order. This occurs both with and without streaming for the function call output.

Issue: Unexpected behavior observed when using function calling with a response schema.

Environment details

  • Programming language: Python
  • OS: macOS
  • Language runtime version: 3.14.2
  • Package version: google-genai 1.55.0

Steps to reproduce

  1. Define a function using google-genai with a JSON schema that specifies a fixed field order.
  2. Call client.models.generate_content_stream() with function calling enabled.
  3. Stream the response and inspect partial function_call.args as they arrive.
  4. Observe that fields are emitted in a different order than defined in the schema.
  5. Confirm the final aggregated function call preserves the same incorrect order.

Expected behavior

Gemini should emit function call arguments that conform exactly to the declared JSON schema,
including respecting the defined field order, both in streaming and non-streaming modes.

Actual behavior

Gemini emits all required fields, but does not respect schema-defined field ordering.
This is observable both during streaming and in the final function call output.

Streaming function-call evidence

Expected order (schema-defined):
['alpha', 'beta', 'gamma']

The following JSON response schema is used for function calling. The schema explicitly defines a fixed property order.

output_response_schema = {
    "type": "object",
    "properties": {
        "alpha": {
            "type": "string",
            "description": "......"
        },
        "beta": {
            "type": "string",
            "description": "......"
        },
        "gamma": {
            "type": "string",
            "description": "......"
        }
    },
    "required": ["alpha", "beta", "gamma"]
}

Using generate_content_stream with include_thoughts=True, function-call fields are emitted incrementally in a non-schema order.

stream = client.models.generate_content_stream(
        model=MODEL_ID,
        contents=[
            types.Content(
                role="user",
                parts=[types.Part(text=SYSTEM_PROMPT + "\n\n" + USER_PROMPT)],
            )
        ],
        config=types.GenerateContentConfig(
            tools=[tool],
            tool_config=tool_config,
            temperature=1.0,
            max_output_tokens=4096,
            thinking_config=types.ThinkingConfig(
                include_thoughts=True
            ),
        ),
    )

Observed streaming order:
beta → gamma → alpha

Final argument key order:
['beta', 'gamma', 'alpha']

--- STREAM END ---
================================================================================

FINAL ANALYSIS
================================================================================
Final argument keys:
['beta', 'gamma', 'alpha']
============================================================
FIELD ORDER ANALYSIS - Streaming Function Calling
============================================================

Desired order: ['alpha', 'beta', 'gamma']
Actual order:  ['beta', 'gamma', 'alpha']

Order matches: ✗ NO

Differences:
  Position 0: expected 'alpha', got 'beta'
  Position 1: expected 'beta', got 'gamma'
  Position 2: expected 'gamma', got 'alpha'

config.py

function_calling_streaming.py

Gemini Function Calling does not preserve response schema field ordering, and this ordering difference is already visible in the streamed output.

Metadata

Metadata

Assignees

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions