Skip to content

feat: openai: allow assistant content to be string#486

Merged
mathetake merged 4 commits intomainfrom
aaron/string-or-assistant-content-openai
Mar 13, 2025
Merged

feat: openai: allow assistant content to be string#486
mathetake merged 4 commits intomainfrom
aaron/string-or-assistant-content-openai

Conversation

@aabchoo
Copy link
Copy Markdown
Contributor

@aabchoo aabchoo commented Mar 13, 2025

Commit Message

Content type should be able to be string so adding this for compatibility. It's already done for the other types - keeping it consistent.

Signed-off-by: Aaron Choo <achoo30@bloomberg.net>
@aabchoo aabchoo requested a review from a team as a code owner March 13, 2025 04:05
@aabchoo aabchoo changed the title feat: assistant content allowed to be string or role content feat: openai: assistant content allowed to be string or role content Mar 13, 2025
@aabchoo aabchoo changed the title feat: openai: assistant content allowed to be string or role content feat: openai: allow assistant content to be string Mar 13, 2025
Signed-off-by: Aaron Choo <achoo30@bloomberg.net>
@aabchoo aabchoo requested a review from yuzisun March 13, 2025 04:18
return nil
}

return fmt.Errorf("cannot unmarshal JSON data as string or assistant content parts")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there's no format then let's use errors.New

Suggested change
return fmt.Errorf("cannot unmarshal JSON data as string or assistant content parts")
return errors.New("cannot unmarshal JSON data as string or assistant content parts")

if content.Type == openai.ChatCompletionAssistantMessageParamContentTypeRefusal {
contentBlocks = append(contentBlocks, &awsbedrock.ContentBlock{Text: content.Refusal})
} else if content.Text != nil {
// TODO: we are sometimes missing the content (should fix)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove this TODO now, this is the fix

Value: openai.ChatCompletionAssistantMessageParam{
Content: openai.ChatCompletionAssistantMessageParamContent{
Text: ptr.To("I dunno"),
Content: openai.StringOrAssistantRoleContentUnion{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you help add the missing unmarshall test which is why this issue is not caught

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue was content being parsed as string even though it was sent as AssistantContentType. Added a test, but the behavior is unideal since the unmarshalling now sees it as valid:

https://github.com/envoyproxy/ai-gateway/pull/486/files#diff-3387664749ae0034b39372c41c009a68c9b68b7ee9ae5886babe0a4f846e542fR168

Signed-off-by: Aaron Choo <achoo30@bloomberg.net>
@mathetake
Copy link
Copy Markdown
Member

LGTM from me

@mathetake mathetake merged commit 14ddb3a into main Mar 13, 2025
17 checks passed
@mathetake mathetake deleted the aaron/string-or-assistant-content-openai branch March 13, 2025 18:01
aabchoo added a commit that referenced this pull request Mar 14, 2025
**Commit Message**

Content type should be able to be string so adding this for
compatibility. It's already done for the other types - keeping it
consistent.

---------

Signed-off-by: Aaron Choo <achoo30@bloomberg.net>
Co-authored-by: Dan Sun <dsun20@bloomberg.net>
aabchoo added a commit that referenced this pull request Mar 14, 2025
**Commit Message**

PR to backport `mockChatCompletionMetrics`, chat completion stream fix,
and openai content type.

Including:
- #459 (468 uses mock components introduced here)
- #468 
- #486

---------

Signed-off-by: Huamin Chen <hchen@redhat.com>
Signed-off-by: Ignasi Barrera <ignasi@tetrate.io>
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
Signed-off-by: Aaron Choo <achoo30@bloomberg.net>
Co-authored-by: Ignasi Barrera <ignasi@tetrate.io>
Co-authored-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
Co-authored-by: Dan Sun <dsun20@bloomberg.net>
mathetake pushed a commit that referenced this pull request Mar 18, 2025
…508)

**Commit Message**

AWS Claude 3.5 v1 has cases when assistant returns empty content.
Sending that back to AWS will result in a validation error. Will skip
adding empty content if string is empty.

```
openai.BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'ValidationException:http://internal.amazon.com/coral/com.amazon.bedrock/', 'code': '400', 'message': 'The text field in the ContentBlock object at messages.3.content.0 is blank. Add text to the text field, and try again.'}}
```


**Related Issues/PRs (if applicable)**
#486

Signed-off-by: Aaron Choo <achoo30@bloomberg.net>
aabchoo added a commit that referenced this pull request Mar 20, 2025
…508)

**Commit Message**

AWS Claude 3.5 v1 has cases when assistant returns empty content.
Sending that back to AWS will result in a validation error. Will skip
adding empty content if string is empty.

```
openai.BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'ValidationException:http://internal.amazon.com/coral/com.amazon.bedrock/', 'code': '400', 'message': 'The text field in the ContentBlock object at messages.3.content.0 is blank. Add text to the text field, and try again.'}}
```


**Related Issues/PRs (if applicable)**
#486

Signed-off-by: Aaron Choo <achoo30@bloomberg.net>
aabchoo added a commit that referenced this pull request Mar 20, 2025
**Commit Message**

Ref: (#508)

AWS Claude 3.5 v1 has cases when assistant returns empty content.
Sending that back to AWS will result in a validation error. Will skip
adding empty content if string is empty.

```
openai.BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'ValidationException:http://internal.amazon.com/coral/com.amazon.bedrock/', 'code': '400', 'message': 'The text field in the ContentBlock object at messages.3.content.0 is blank. Add text to the text field, and try again.'}}
```


**Related Issues/PRs (if applicable)**
#486

Signed-off-by: Aaron Choo <achoo30@bloomberg.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants