Unsupported JSON schema type: ['string', 'null']

Hi,

I’ve been exploring the function calling capability of your API and have encountered an issue related to the handling of optional fields in the JSON schema.

Specifically, your models occasionally set optional inputs to null instead of omitting them. This results in “Invalid function calling output” errors from the API.

To address this, I attempted to make these fields nullable using the syntax specified in your documentation:

"type": ["string", "null"]

However, this results in the following error:

Unsupported JSON schema type: ['string', 'null']

Would it be possible to update the behavior so that if an optional parameter is returned with a null value, it is automatically omitted from the output rather than triggering an invalid input error?

Thank you.

Hi @lfitzy95 ,
Thank you for highlighting this issue. We acknowledge the challenge with null values in optional fields and the current lack of support for ["string", "null"] types. We’ll share this feedback with our engineering team for consideration in future updates.

Regards,
Shivani

Hi @lfitzy95 ,
After further investigation, we were able to identify a solution that effectively resolves the issue. By setting "strict": true and "additionalProperties": true in the schema, and ensuring that "enum" declarations are correctly defined where necessary, the function calling mechanism now handles optional fields gracefully. This configuration prevents validation errors when optional parameters are returned as null, allowing the model to function as intended.

Please refer below example code:

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get weather of an location, the user shoud supply a location first",
            "strict": True,
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {
                        "type": ["string","null"],
                        "enum": ["Paris", "London"],
                        "description": "The city and state, e.g. San Francisco, CA",
                    }
                },
                "required": ["city"],
                "additionalProperties": False
            },
        }
    },
]

End-to-End Example:

import openai
import cmath
import random
import json

# Define the OpenAI client
client = openai.OpenAI(
    base_url="https://api.sambanova.ai/v1", 
    api_key="SAMBANOVA_API_KEY"
)

MODEL = 'Meta-Llama-3.3-70B-Instruct'

def get_weather(city: str) -> dict: 
    """
    Fake weather lookup: returns a random temperature between 20°C and 50°C.
    """
    temp = random.randint(20, 50)
    return {
        "city": city,
        "temperature_celsius": temp
    }


tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get weather of an location, the user shoud supply a location first",
            "strict": True,
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {
                        "type": ["string","null"],
                        "enum": ["Paris", "London"],
                        "description": "The city and state, e.g. San Francisco, CA",
                    }
                },
                "required": ["city"],
                "additionalProperties": False
            },
        }
    },
]

messages = [{"role": "user", "content": "What's the weather like in Paris today?"}]


completion = client.chat.completions.create(
    model=MODEL,
    messages=messages,
    tools=tools
)

print(completion)


tool_call = completion.choices[0].message.tool_calls[0]
args = json.loads(tool_call.function.arguments)

result = get_weather(args["city"])


messages.append(completion.choices[0].message)  # append model's function call message
messages.append({                               # append result message
    "role": "tool",
    "tool_call_id": tool_call.id,
    "content": str(result)
})

completion_2 = client.chat.completions.create(
    model=MODEL,
    messages=messages,
 
)
print(completion_2.choices[0].message.content)

Best Regards,
Shivani

1 Like