Skip to content

Commit

Permalink
[Bug] [Assistant API] - Do not allow empty conversation ID in chat/co…
Browse files Browse the repository at this point in the history
…mplete route (#11783) (#213049)

## Summary

BUG: elastic/security-team#11783

This PR fixes the behaviour of the
`/api/security_ai_assistant/chat/complete` route where the
`conversationId` can be passed as an empty string. This may lead to
unexpected results described in
elastic/security-team#11783 (comment).

### Expected behaviour

We should throw a bad request (400) http error when empty
`conversationId` has been passed.

### Testing

* Use this `curl` command to test the endpoint.

```
curl --location 'http://localhost:5601/api/security_ai_assistant/chat/complete' \
--header 'kbn-xsrf: true' \
--header 'Content-Type: application/json' \
--data '{
  "connectorId": "{{my-gpt4o-ai}}",
  "conversationId": "",
  "isStream": false,
  "messages": [
    {
      "content": "Follow up",
      "role": "user"
    }
  ],
  "persist": true
}'
```

You should see next error as a response:

```
{
    "statusCode": 400,
    "error": "Bad Request",
    "message": "[request body]: conversationId: String must contain at least 1 character(s), conversationId: No empty strings allowed"
}
```

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
(cherry picked from commit 7db897a)
  • Loading branch information
e40pud committed Mar 4, 2025
1 parent a37f1d3 commit 309f513
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion oas_docs/output/kibana.serverless.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42966,7 +42966,7 @@ components:
connectorId:
type: string
conversationId:
type: string
$ref: '#/components/schemas/Security_AI_Assistant_API_NonEmptyString'
isStream:
type: boolean
langSmithApiKey:
Expand Down
2 changes: 1 addition & 1 deletion oas_docs/output/kibana.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34580,7 +34580,7 @@ components:
connectorId:
type: string
conversationId:
type: string
$ref: '#/components/schemas/Security_AI_Assistant_API_NonEmptyString'
isStream:
type: boolean
langSmithApiKey:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ components:
connectorId:
type: string
conversationId:
type: string
$ref: '#/components/schemas/NonEmptyString'
isStream:
type: boolean
langSmithApiKey:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ components:
connectorId:
type: string
conversationId:
type: string
$ref: '#/components/schemas/NonEmptyString'
isStream:
type: boolean
langSmithApiKey:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import { z } from '@kbn/zod';

import { NonEmptyString } from '../common_attributes.gen';

export type RootContext = z.infer<typeof RootContext>;
export const RootContext = z.literal('security');

Expand Down Expand Up @@ -52,7 +54,7 @@ export const ChatMessage = z.object({

export type ChatCompleteProps = z.infer<typeof ChatCompleteProps>;
export const ChatCompleteProps = z.object({
conversationId: z.string().optional(),
conversationId: NonEmptyString.optional(),
promptId: z.string().optional(),
isStream: z.boolean().optional(),
responseLanguage: z.string().optional(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ components:
type: object
properties:
conversationId:
type: string
$ref: '../common_attributes.schema.yaml#/components/schemas/NonEmptyString'
promptId:
type: string
isStream:
Expand All @@ -103,7 +103,7 @@ components:
messages:
type: array
items:
$ref: '#/components/schemas/ChatMessage'
$ref: '#/components/schemas/ChatMessage'
required:
- messages
- persist
Expand Down

0 comments on commit 309f513

Please sign in to comment.