Skip to content

Commit 9a1b723

Browse files
committed
fix(sdk): cap UI-TARS 1.5 default output tokens
1 parent 7986f5a commit 9a1b723

2 files changed

Lines changed: 93 additions & 1 deletion

File tree

packages/ui-tars/sdk/src/Model.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ type OpenAIChatCompletionCreateParams = Omit<ClientOptions, 'maxRetries'> &
3737
'model' | 'max_tokens' | 'temperature' | 'top_p'
3838
>;
3939

40+
const DEFAULT_MAX_TOKENS = 1000;
41+
const DEFAULT_MAX_TOKENS_V1_5 = 8192;
42+
4043
export interface UITarsModelConfig extends OpenAIChatCompletionCreateParams {
4144
/** Whether to use OpenAI Response API instead of Chat Completions API */
4245
useResponsesApi?: boolean;
@@ -107,7 +110,9 @@ export class UITarsModel extends Model {
107110
baseURL,
108111
apiKey,
109112
model,
110-
max_tokens = uiTarsVersion == UITarsModelVersion.V1_5 ? 65535 : 1000,
113+
max_tokens = uiTarsVersion === UITarsModelVersion.V1_5
114+
? DEFAULT_MAX_TOKENS_V1_5
115+
: DEFAULT_MAX_TOKENS,
111116
temperature = 0,
112117
top_p = 0.7,
113118
...restOptions

packages/ui-tars/sdk/tests/Model.test.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
import { describe, expect, it, vi, beforeEach } from 'vitest';
66
import { UITarsModel } from '../src/Model';
7+
import { UITarsModelVersion } from '@ui-tars/shared/types';
78

89
// Mock OpenAI
910
const mockCreate = vi.fn();
@@ -111,6 +112,63 @@ describe('UITarsModel', () => {
111112
expect.any(Object),
112113
);
113114
});
115+
116+
it('uses a provider-safe default max_tokens for UI-TARS 1.5', async () => {
117+
const model = new UITarsModel({
118+
apiKey: 'test-key',
119+
baseURL: 'https://test.com',
120+
model: 'test-model',
121+
useResponsesApi: false,
122+
});
123+
124+
mockCreate.mockResolvedValue({
125+
choices: [{ message: { content: 'test response' } }],
126+
usage: { total_tokens: 100 },
127+
});
128+
129+
await model.invoke({
130+
conversations: [{ from: 'human', value: 'test' }],
131+
images: [],
132+
screenContext: { width: 1920, height: 1080 },
133+
uiTarsVersion: UITarsModelVersion.V1_5,
134+
});
135+
136+
expect(mockCreate).toHaveBeenCalledWith(
137+
expect.objectContaining({
138+
max_tokens: 8192,
139+
}),
140+
expect.any(Object),
141+
);
142+
});
143+
144+
it('preserves explicit max_tokens overrides for UI-TARS 1.5', async () => {
145+
const model = new UITarsModel({
146+
apiKey: 'test-key',
147+
baseURL: 'https://test.com',
148+
model: 'test-model',
149+
useResponsesApi: false,
150+
max_tokens: 12000,
151+
});
152+
153+
mockCreate.mockResolvedValue({
154+
choices: [{ message: { content: 'test response' } }],
155+
usage: { total_tokens: 100 },
156+
});
157+
158+
await model.invoke({
159+
conversations: [{ from: 'human', value: 'test' }],
160+
images: [],
161+
screenContext: { width: 1920, height: 1080 },
162+
uiTarsVersion: UITarsModelVersion.V1_5,
163+
});
164+
165+
expect(mockCreate).toHaveBeenCalledWith(
166+
expect.objectContaining({
167+
max_tokens: 12000,
168+
}),
169+
expect.any(Object),
170+
);
171+
});
114172
});
115173

116174
describe('Response API', () => {
@@ -175,6 +233,35 @@ describe('UITarsModel', () => {
175233
);
176234
});
177235

236+
it('uses the same safe UI-TARS 1.5 default for Response API max_output_tokens', async () => {
237+
const model = new UITarsModel({
238+
apiKey: 'test-key',
239+
baseURL: 'https://test.com',
240+
model: 'test-model',
241+
useResponsesApi: true,
242+
});
243+
244+
mockResponsesCreate.mockResolvedValue({
245+
id: 'response-1',
246+
output_text: 'test response',
247+
usage: { total_tokens: 50 },
248+
});
249+
250+
await model.invoke({
251+
conversations: [{ from: 'human', value: 'test' }],
252+
images: [],
253+
screenContext: { width: 1920, height: 1080 },
254+
uiTarsVersion: UITarsModelVersion.V1_5,
255+
});
256+
257+
expect(mockResponsesCreate).toHaveBeenCalledWith(
258+
expect.objectContaining({
259+
max_output_tokens: 8192,
260+
}),
261+
expect.any(Object),
262+
);
263+
});
264+
178265
it('should send incremental messages for Response API (subsequent call)', async () => {
179266
const model = new UITarsModel({
180267
apiKey: 'test-key',

0 commit comments

Comments
 (0)