Module: DataRedactor::Integrations::OpenAI
- Defined in:
- lib/data_redactor/integrations/openai.rb
Overview
Adapter for OpenAI Chat Completions payloads. Scrubs PII and secrets from
a request's messages before they leave the process, and from a
response's choices[].message.content before they're logged or stored.
Operates on plain Ruby Hashes/Arrays with either String or Symbol keys,
so it works with the openai gem, a raw HTTP client, or parsed JSON — no
runtime dependency on any SDK. Inputs are never mutated; a deep copy is
returned.
Class Method Summary collapse
-
.redact_messages(messages, only: nil, except: nil, placeholder: DataRedactor::PLACEHOLDER_DEFAULT) ⇒ Array<Hash>, Hash
Redact an OpenAI
messagesarray before sending the request. -
.redact_response(response, only: nil, except: nil, placeholder: DataRedactor::PLACEHOLDER_DEFAULT) ⇒ Hash
Redact an OpenAI Chat Completions response before logging or storing it.
Class Method Details
.redact_messages(messages, only: nil, except: nil, placeholder: DataRedactor::PLACEHOLDER_DEFAULT) ⇒ Array<Hash>, Hash
Redact an OpenAI messages array before sending the request. Returns a
deep copy; the input is not mutated.
Each message's content may be a String or an array of parts
({ type: "text", text: "..." }); only the text field of text
parts is redacted. Non-text parts (e.g. image_url) pass through
untouched. A { role: "system", content: ... } entry is redacted like
any other message (OpenAI carries the system prompt in the array).
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/data_redactor/integrations/openai.rb', line 48 def (, only: nil, except: nil, placeholder: DataRedactor::PLACEHOLDER_DEFAULT) redact = ->(s) { DataRedactor.redact(s, only: only, except: except, placeholder: placeholder) } if .is_a?(Hash) out = LLMSupport.deep_copy() list = LLMSupport.fetch(out, :messages) LLMSupport.put(out, :messages, (list, redact)) if list.is_a?(Array) out else (LLMSupport.deep_copy(), redact) end end |
.redact_response(response, only: nil, except: nil, placeholder: DataRedactor::PLACEHOLDER_DEFAULT) ⇒ Hash
Redact an OpenAI Chat Completions response before logging or storing it. Returns a deep copy; the input is not mutated.
Walks choices[].message.content and redacts each (String content),
leaving the rest of the response (id, usage, finish_reason) intact.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/data_redactor/integrations/openai.rb', line 79 def redact_response(response, only: nil, except: nil, placeholder: DataRedactor::PLACEHOLDER_DEFAULT) redact = ->(s) { DataRedactor.redact(s, only: only, except: except, placeholder: placeholder) } out = LLMSupport.deep_copy(response) return out unless out.is_a?(Hash) choices = LLMSupport.fetch(out, :choices) return out unless choices.is_a?(Array) choices.each do |choice| next unless choice.is_a?(Hash) = LLMSupport.fetch(choice, :message) next unless .is_a?(Hash) content = LLMSupport.fetch(, :content) LLMSupport.put(, :content, redact.call(content)) if content.is_a?(String) end out end |