Skip to content

Commit b1bbf31

Browse files
fix(ai): keep refusal content in OpenAI Responses API output (#5563)
* fix(ai): keep refusal content in OpenAI Responses API output * test(webpack-plugin): allow production builds time to finish in CI --------- Co-authored-by: Manoel Aranda Neto <marandaneto@gmail.com>
1 parent 7be658c commit b1bbf31

4 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎.changeset/ai-responses-refusal.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@posthog/ai': patch
3+
---
4+
5+
Keep the refusal text of OpenAI Responses API outputs instead of dropping it from the captured `$ai_output_choices`

‎packages/ai/src/utils.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ export const formatResponseOpenAI = (response: any): FormattedMessage[] => {
260260
content.push({ type: 'text', text: contentItem.text })
261261
} else if (contentItem.text) {
262262
content.push({ type: 'text', text: contentItem.text })
263+
} else if (contentItem.type === 'refusal' && contentItem.refusal) {
264+
content.push({ type: 'refusal', refusal: contentItem.refusal })
263265
} else if (contentItem.type === 'input_image' && contentItem.image_url) {
264266
content.push({
265267
type: 'image',

‎packages/ai/tests/tools.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,22 @@ describe('Tool Handling Tests', () => {
196196
])
197197
})
198198

199+
it('should keep the refusal of a refused Responses API request', () => {
200+
const response = {
201+
output: [
202+
{
203+
type: 'message',
204+
role: 'assistant',
205+
content: [{ type: 'refusal', refusal: "I can't help with that" }],
206+
},
207+
],
208+
}
209+
210+
expect(formatResponseOpenAI(response)).toEqual([
211+
{ role: 'assistant', content: [{ type: 'refusal', refusal: "I can't help with that" }] },
212+
])
213+
})
214+
199215
it('should format response with only tool calls', () => {
200216
const response = {
201217
choices: [

‎packages/webpack-plugin/vitest.config.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export default defineConfig({
1111
globals: true,
1212
clearMocks: true,
1313
environment: 'node',
14+
// Production webpack builds can exceed the default five seconds on shared CI runners.
15+
testTimeout: 30_000,
1416
exclude: [...configDefaults.exclude, 'dist/**'],
1517
},
1618
})

0 commit comments

Comments
 (0)