Skip to content

support for Decimal in google gemini genai #1708

@oegedijk

Description

@oegedijk
  • This is actually a bug report.
  • I am not getting good LLM Results
  • I have tried asking for help in the community on discord or discussions and have not received a response.
  • I have tried searching the documentation and have not found an answer.

What Model are you using?

  • gpt-3.5-turbo
  • gpt-4-turbo
  • gpt-4
  • Other (gemini-2.5-flash)

Describe the bug
When using a Decimal in a schema instructor complains that genai does not support Optional types, even though the genai client seems to work with Decimals just fine?

To Reproduce

from decimal import Decimal

import instructor
from google import genai
from google.genai import types
from pydantic import BaseModel

genai_client = genai.Client()


class Receipt(BaseModel):
    total: Decimal


response = genai_client.models.generate_content(
    model="gemini-2.5-flash",
    contents=[
        "Extract out the total and line items from the invoice",
        "the total on the receipt is 100.34",
    ],
    config=types.GenerateContentConfig(
        response_mime_type="application/json",
        response_schema=Receipt,
    ),
)

print(response.text)

for mode in [instructor.Mode.GENAI_STRUCTURED_OUTPUTS, instructor.Mode.GENAI_TOOLS]:
    try:
        instructor_client = instructor.from_genai(genai_client, mode=mode)

        response = instructor_client.chat.completions.create(
            model="gemini-2.5-flash",
            response_model=Receipt,
            messages=[
                {
                    "role": "user",
                    "content": [
                        "Extract out the total and line items from the invoice",
                        "the total on the receipt is 100.34",
                    ],
                },
            ],
            )

        print(response)
    except Exception as e:
        print(e)
        print(f"Mode {mode} failed")

results in:

{"total":100.34}
Gemini does not support Union types (except Optional). Please change your function schema
Mode Mode.GENAI_STRUCTURED_OUTPUTS failed
Gemini does not support Union types (except Optional). Please change your function schema
Mode Mode.GENAI_TOOLS failed

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions