File size: 2,059 Bytes
a4b70d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Reasoning Field Standardization

## Issue
DeepSeek uses `"reasoning_content"` field while OpenAI uses `"reasoning"` field in their chat completion streaming responses. This inconsistency caused confusion about what field name to use in the g4f Interference API.

## Decision
**Standardized on OpenAI's `"reasoning"` field format for API output while maintaining input compatibility.**

## Rationale
1. **OpenAI Compatibility**: OpenAI is the de facto standard for chat completion APIs
2. **Ecosystem Compatibility**: Most tools and libraries expect OpenAI format
3. **Consistency**: Provides a unified output format regardless of the underlying provider
4. **Backward Compatibility**: Input parsing continues to accept both formats

## Implementation

### Input Format Support (Unchanged)
The system continues to accept both input formats in `OpenaiTemplate.py`:
```python
reasoning_content = choice.get("delta", {}).get("reasoning_content", choice.get("delta", {}).get("reasoning"))
```

### Output Format Standardization (Changed)
- **Streaming Delta**: Uses `reasoning` field (OpenAI format)
- **Non-streaming Message**: Uses `reasoning` field (OpenAI format)  
- **API Responses**: Should use standard OpenAI streaming format

### Example Output Formats

#### Streaming Response (OpenAI Compatible)
```json
{
  "id": "chatcmpl-example",
  "object": "chat.completion.chunk",
  "choices": [{
    "index": 0,
    "delta": {
      "role": "assistant",
      "reasoning": "I need to think about this step by step..."
    },
    "finish_reason": null
  }]
}
```

#### Non-streaming Response
```json
{
  "choices": [{
    "message": {
      "role": "assistant",
      "content": "Here's my answer",
      "reasoning": "My reasoning process was..."
    }
  }]
}
```

## Files Changed
- `g4f/client/stubs.py`: Updated to use `reasoning` field instead of `reasoning_content`

## Testing
- Added comprehensive tests for format standardization
- Verified input compatibility with both OpenAI and DeepSeek formats
- Confirmed no regressions in existing functionality