-
Notifications
You must be signed in to change notification settings - Fork 16.4k
Open
Description
[BUG] DeepSeek API Strict Mode Returns Malformed JSON in Function Call Arguments
Describe the bug
When using strict: true in the function/tool definition with DeepSeek API (beta endpoint), the API returns malformed JSON in the function.arguments field. Specifically, the first property key is missing its closing double quote, making the JSON unparseable.
Example of malformed response:
{"selected: ["A", "C", "D"]}Instead of the expected valid JSON:
{"selected": ["A", "C", "D"]}This causes json.loads() to fail with:
JSONDecodeError: Expecting ':' delimiter: line 1 column 15 (char 14)
To Reproduce
Minimal Reproducible Code
from openai import OpenAI
# Initialize client with beta endpoint
client = OpenAI(
api_key="<your-api-key>",
base_url="https://api.deepseek.com/beta"
)
# Define tool with strict=True
tools = [{
"type": "function",
"function": {
"name": "submit_answer",
"description": "Submit multiple choice answer",
"strict": True, # ← This triggers the bug
"parameters": {
"type": "object",
"properties": {
"selected": {
"type": "array",
"description": "Selected options",
"items": {"type": "string"}
}
},
"required": ["selected"],
"additionalProperties": False
}
}
}]
# Send request
response = client.chat.completions.create(
model="deepseek-chat",
messages=[{
"role": "user",
"content": "Which are Python core features? A. Dynamic typing B. Compiled language C. OOP D. Multithreading. Use submit_answer tool."
}],
tools=tools,
tool_choice="auto"
)
# Try to parse the arguments
import json
arguments_raw = response.choices[0].message.tool_calls[0].function.arguments
print(f"Raw arguments: {arguments_raw}")
# This will fail with JSONDecodeError
args = json.loads(arguments_raw) # ❌ Crashes hereSteps to reproduce:
- Use DeepSeek API with
/betaendpoint - Define a function/tool with
strict: true - Include
additionalProperties: falsein parameters schema - Make an API call that triggers function calling
- Attempt to parse the returned
function.argumentsas JSON
Expected behavior
The API should return valid JSON in function.arguments that can be parsed with standard JSON parsers:
{
"selected": ["A", "C", "D"]
}Screenshots
Comparison Test Results
Test Code:
def test_strict_mode(strict: bool):
tools = [{
"type": "function",
"function": {
"name": "submit_answer",
"strict": strict, # Test with True/False
"parameters": {
"type": "object",
"properties": {
"selected": {
"type": "array",
"items": {"type": "string"}
}
},
"required": ["selected"],
"additionalProperties": False
}
}
}]
# ... rest of the codeOutput:
======================================================================
Testing with strict=False
======================================================================
✓ Tool Called: submit_answer
Arguments (raw): {"selected": ["A", "C", "D"]}
✅ JSON Valid
Selected: ['A', 'C', 'D']
======================================================================
Testing with strict=True
======================================================================
✓ Tool Called: submit_answer
Arguments (raw): {"selected: ["A", "C", "D"]}
❌ JSON Invalid: Expecting ':' delimiter: line 1 column 15 (char 14)
Error Position: char 14
Context: ...lected: ["A", "C", "...
Key Observation: The only difference between the two tests is strict: True vs strict: False. With strict=True, the closing quote is missing after "selected.
Additional context
Environment
- API Endpoint:
https://api.deepseek.com/beta - Model:
deepseek-chat - SDK: OpenAI Python SDK (official)
- Python Version: 3.12.11
- Date Tested: 2025-12-28
Impact
- Severity: High - Completely breaks function calling when using strict mode
- Workaround: Disable
strict: true(usestrict: falseor omit the parameter) - Affected Use Cases: Any application relying on structured output validation via strict mode
Additional Notes
- This issue is specific to the
/betaendpoint - The same tool definition works correctly when
strict: false(default) - Tested with the official OpenAI SDK (not third-party wrappers like LiteLLM)
- The malformed JSON always has the same pattern: missing closing quote on the first property key
Related
- Similar to function calling issues seen in other LLM APIs, but this is specifically triggered by the
strictparameter - DeepSeek's strict mode is meant to enforce schema compliance (similar to OpenAI's structured outputs), but the implementation appears to have a JSON serialization bug
Let me know if you need the complete test scripts or additional reproduction details.
Metadata
Metadata
Assignees
Labels
No labels