-
Notifications
You must be signed in to change notification settings - Fork 3
fix: consider only last input item when prompt recording for responses API #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+175
−90
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a70e3d9
fix: consider only last input item when prompt recording for response…
pawbana a7aa465
review: added test case with mutiple text input items
pawbana 0fa97e8
review: fix happy path indent
pawbana ff4bd28
review 2: new line between text_input items
pawbana 18a5f46
fix not recording prompt in streaming + MCP call case (TestResponsesI…
pawbana 77f0145
review: added comment about i.promptWasRecorded
pawbana 7cd5662
review: extract prompt in interceptor + record it once after inner loop
pawbana File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -139,66 +139,87 @@ func (i *responsesInterceptionBase) requestOptions(respCopy *responseCopier) []o | |
| return opts | ||
| } | ||
|
|
||
| // lastUserPrompt returns last input message with "user" role | ||
| func (i *responsesInterceptionBase) lastUserPrompt() (string, error) { | ||
| // lastUserPrompt returns input text with "user" role from last input item | ||
| // or string input value if it is present + bool indicating if input was found or not. | ||
| // If no such input was found empty string + false is returned. | ||
| func (i *responsesInterceptionBase) lastUserPrompt(ctx context.Context) (string, bool, error) { | ||
| if i == nil { | ||
| return "", errors.New("cannot get last user prompt: nil struct") | ||
| return "", false, errors.New("cannot get last user prompt: nil struct") | ||
| } | ||
| if i.req == nil { | ||
| return "", errors.New("cannot get last user prompt: nil req struct") | ||
| return "", false, errors.New("cannot get last user prompt: nil request struct") | ||
| } | ||
|
|
||
| // 'input' field can be a string or array of objects: | ||
| // https://platform.openai.com/docs/api-reference/responses/create#responses_create-input | ||
|
|
||
| // Check string variant | ||
| if i.req.Input.OfString.Valid() { | ||
| return i.req.Input.OfString.Value, nil | ||
| return i.req.Input.OfString.Value, true, nil | ||
| } | ||
|
|
||
| // Fallback to parsing original bytes since golang SDK doesn't properly decode 'Input' field. | ||
| // If 'type' field of input item is not set it will be omitted from 'Input.OfInputItemList' | ||
| // It is an optional field according to API: https://platform.openai.com/docs/api-reference/responses/create#responses_create-input-input_item_list-input_message | ||
| // example: fixtures/openai/responses/blocking/builtin_tool.txtar | ||
| inputItems := gjson.GetBytes(i.reqPayload, "input").Array() | ||
| for i := len(inputItems) - 1; i >= 0; i-- { | ||
| item := inputItems[i] | ||
| if item.Get("role").Str == string(constant.ValueOf[constant.User]()) { | ||
| var sb strings.Builder | ||
|
|
||
| // content can be a string or array of objects: | ||
| // https://platform.openai.com/docs/api-reference/responses/create#responses_create-input-input_item_list-input_message-content | ||
| content := item.Get("content") | ||
| if content.Str != "" { | ||
| return content.Str, nil | ||
| } | ||
| for _, c := range content.Array() { | ||
| if c.Get("type").Str == "input_text" { | ||
| sb.WriteString(c.Get("text").Str) | ||
| } | ||
| } | ||
| if sb.Len() > 0 { | ||
| return sb.String(), nil | ||
| } | ||
| inputItems := gjson.GetBytes(i.reqPayload, "input") | ||
|
|
||
| if !inputItems.IsArray() { | ||
| if inputItems.Type == gjson.Null { | ||
| return "", false, nil | ||
| } | ||
| return "", false, fmt.Errorf("unexpected input type: %v", inputItems.Type.String()) | ||
| } | ||
|
|
||
| inputItemsArr := inputItems.Array() | ||
| if len(inputItemsArr) == 0 { | ||
| return "", false, nil | ||
| } | ||
| lastItem := inputItemsArr[len(inputItemsArr)-1] | ||
|
|
||
| // Request was likely not human-initiated. | ||
| return "", nil | ||
| } | ||
| if lastItem.Get("role").Str != string(constant.ValueOf[constant.User]()) { | ||
| return "", false, nil | ||
| } | ||
|
|
||
| func (i *responsesInterceptionBase) recordUserPrompt(ctx context.Context, responseID string) { | ||
| prompt, err := i.lastUserPrompt() | ||
| if err != nil { | ||
| i.logger.Warn(ctx, "failed to get last user prompt", slog.Error(err)) | ||
| return | ||
| // content can be a string or array of objects: | ||
| // https://platform.openai.com/docs/api-reference/responses/create#responses_create-input-input_item_list-input_message-content | ||
| content := lastItem.Get(string(constant.ValueOf[constant.Content]())) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe a helper function? I see |
||
|
|
||
| // non array case, should be string | ||
| if !content.IsArray() { | ||
| if content.Type == gjson.String { | ||
| return content.Str, true, nil | ||
| } | ||
| return "", false, fmt.Errorf("unexpected input content type: %v", content.Type.String()) | ||
| } | ||
|
|
||
| // No prompt found: last request was not human-initiated. | ||
| if prompt == "" { | ||
| return | ||
| var sb strings.Builder | ||
| promptExists := false | ||
| for _, c := range content.Array() { | ||
| // ignore inputs of not `input_text` type | ||
| if c.Get(string(constant.ValueOf[constant.Type]())).Str != string(constant.ValueOf[constant.InputText]()) { | ||
| continue | ||
| } | ||
|
|
||
| text := c.Get(string(constant.ValueOf[constant.Text]())) | ||
| if text.Type == gjson.String { | ||
| promptExists = true | ||
| sb.WriteString(text.Str + "\n") | ||
| } else { | ||
| i.logger.Warn(ctx, fmt.Sprintf("unexpected input content array element text type: %v", text.Type)) | ||
| } | ||
| } | ||
|
|
||
| if !promptExists { | ||
| return "", false, nil | ||
| } | ||
|
|
||
| prompt := strings.TrimSuffix(sb.String(), "\n") | ||
| return prompt, true, nil | ||
| } | ||
|
|
||
| func (i *responsesInterceptionBase) recordUserPrompt(ctx context.Context, responseID string, prompt string) { | ||
| if responseID == "" { | ||
| i.logger.Warn(ctx, "got empty response ID, skipping prompt recording") | ||
| return | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I would hide the expression like this behind a descriptive function