Skip to content

Guardrails request config validation #371

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
merged 20 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub struct DetectorConfig {
pub r#type: DetectorType,
}

#[derive(Default, Clone, Debug, Deserialize)]
#[derive(Default, Clone, Debug, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum DetectorType {
Expand Down
41 changes: 41 additions & 0 deletions src/orchestrator/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
*/
use std::{collections::HashMap, sync::Arc};

use tracing::error;

use crate::{
clients::chunker::DEFAULT_CHUNKER_ID,
config::{DetectorConfig, DetectorType},
models::DetectorParams,
orchestrator::{Context, Error},
};
Expand Down Expand Up @@ -112,6 +116,43 @@ pub fn configure_mock_servers(
};
}

/// Validates guardrails on request.
pub fn validate_detectors(
detectors: &HashMap<String, DetectorParams>,
orchestrator_detectors: &HashMap<String, DetectorConfig>,
allowed_detector_types: &[DetectorType],
allows_whole_doc_chunker: bool,
) -> Result<(), Error> {
let whole_doc_chunker_id = DEFAULT_CHUNKER_ID;
for detector_id in detectors.keys() {
// validate detectors
match orchestrator_detectors.get(detector_id) {
Some(detector_config) => {
if !allowed_detector_types.contains(&detector_config.r#type) {
let error = Error::Validation(format!(
"detector `{detector_id}` is not supported by this endpoint"
));
error!("{error}");
return Err(error);
}
if !allows_whole_doc_chunker && detector_config.chunker_id == whole_doc_chunker_id {
let error = Error::Validation(format!(
"detector `{detector_id}` uses chunker `whole_doc_chunker`, which is not supported by this endpoint"
));
error!("{error}");
return Err(error);
}
}
None => {
let error = Error::DetectorNotFound(detector_id.clone());
error!("{error}");
return Err(error);
}
}
}
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
21 changes: 19 additions & 2 deletions src/orchestrator/handlers/chat_completions_detection/unary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ use uuid::Uuid;
use super::ChatCompletionsDetectionTask;
use crate::{
clients::openai::*,
config::DetectorType,
models::{
DetectionWarningReason, DetectorParams, UNSUITABLE_INPUT_MESSAGE, UNSUITABLE_OUTPUT_MESSAGE,
},
orchestrator::{Context, Error, common, types::ChatMessageIterator},
orchestrator::{
Context, Error,
common::{self, validate_detectors},
types::ChatMessageIterator,
},
};

pub async fn handle_unary(
Expand All @@ -39,7 +44,19 @@ pub async fn handle_unary(
let input_detectors = detectors.input;
let output_detectors = detectors.output;

// TODO: validate requested guardrails
validate_detectors(
&input_detectors,
&ctx.config.detectors,
&[DetectorType::TextContents],
true,
)?;

validate_detectors(
&output_detectors,
&ctx.config.detectors,
&[DetectorType::TextContents],
true,
)?;

if !input_detectors.is_empty() {
// Handle input detection
Expand Down
13 changes: 11 additions & 2 deletions src/orchestrator/handlers/chat_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ use tracing::{info, instrument};
use super::Handle;
use crate::{
clients::openai,
config::DetectorType,
models::{ChatDetectionHttpRequest, ChatDetectionResult, DetectorParams},
orchestrator::{Error, Orchestrator, common},
orchestrator::{
Error, Orchestrator,
common::{self, validate_detectors},
},
};

impl Handle<ChatDetectionTask> for Orchestrator {
Expand All @@ -40,7 +44,12 @@ impl Handle<ChatDetectionTask> for Orchestrator {
let trace_id = task.trace_id;
info!(%trace_id, config = ?task.detectors, "task started");

// TODO: validate requested guardrails
validate_detectors(
&task.detectors,
&ctx.config.detectors,
&[DetectorType::TextChat],
true,
)?;

// Handle detection
let detections = common::text_chat_detections(
Expand Down
21 changes: 19 additions & 2 deletions src/orchestrator/handlers/classification_with_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ use tracing::{error, info, instrument};
use super::Handle;
use crate::{
clients::GenerationClient,
config::DetectorType,
models::{
ClassifiedGeneratedTextResult, DetectionWarning, DetectorParams, GuardrailsConfig,
GuardrailsHttpRequest, GuardrailsTextGenerationParameters,
TextGenTokenClassificationResults,
},
orchestrator::{Context, Error, Orchestrator, common},
orchestrator::{
Context, Error, Orchestrator,
common::{self, validate_detectors},
},
};

impl Handle<ClassificationWithGenTask> for Orchestrator {
Expand All @@ -47,7 +51,20 @@ impl Handle<ClassificationWithGenTask> for Orchestrator {
let input_detectors = task.guardrails_config.input_detectors();
let output_detectors = task.guardrails_config.output_detectors();

// TODO: validate requested guardrails
// input detectors validation
validate_detectors(
&input_detectors,
&ctx.config.detectors,
&[DetectorType::TextContents],
true,
)?;
// output detectors validation
validate_detectors(
&output_detectors,
&ctx.config.detectors,
&[DetectorType::TextContents],
true,
)?;

if !input_detectors.is_empty() {
// Handle input detection
Expand Down
13 changes: 11 additions & 2 deletions src/orchestrator/handlers/context_docs_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ use tracing::{info, instrument};
use super::Handle;
use crate::{
clients::detector::ContextType,
config::DetectorType,
models::{ContextDocsHttpRequest, ContextDocsResult, DetectorParams},
orchestrator::{Error, Orchestrator, common},
orchestrator::{
Error, Orchestrator,
common::{self, validate_detectors},
},
};

impl Handle<ContextDocsDetectionTask> for Orchestrator {
Expand All @@ -40,7 +44,12 @@ impl Handle<ContextDocsDetectionTask> for Orchestrator {
let trace_id = task.trace_id;
info!(%trace_id, config = ?task.detectors, "task started");

// TODO: validate requested guardrails
validate_detectors(
&task.detectors,
&ctx.config.detectors,
&[DetectorType::TextContextDoc],
true,
)?;

// Handle detection
let detections = common::text_context_detections(
Expand Down
13 changes: 11 additions & 2 deletions src/orchestrator/handlers/detection_on_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ use tracing::{info, instrument};

use super::Handle;
use crate::{
config::DetectorType,
models::{DetectionOnGeneratedHttpRequest, DetectionOnGenerationResult, DetectorParams},
orchestrator::{Error, Orchestrator, common},
orchestrator::{
Error, Orchestrator,
common::{self, validate_detectors},
},
};

impl Handle<DetectionOnGenerationTask> for Orchestrator {
Expand All @@ -39,7 +43,12 @@ impl Handle<DetectionOnGenerationTask> for Orchestrator {
let trace_id = task.trace_id;
info!(%trace_id, config = ?task.detectors, "task started");

// TODO: validate requested guardrails
validate_detectors(
&task.detectors,
&ctx.config.detectors,
&[DetectorType::TextGeneration],
true,
)?;

// Handle detection
let detections = common::text_generation_detections(
Expand Down
13 changes: 11 additions & 2 deletions src/orchestrator/handlers/generation_with_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ use tracing::{info, instrument};
use super::Handle;
use crate::{
clients::GenerationClient,
config::DetectorType,
models::{
DetectorParams, GenerationWithDetectionHttpRequest, GenerationWithDetectionResult,
GuardrailsTextGenerationParameters,
},
orchestrator::{Error, Orchestrator, common},
orchestrator::{
Error, Orchestrator,
common::{self, validate_detectors},
},
};

impl Handle<GenerationWithDetectionTask> for Orchestrator {
Expand All @@ -43,7 +47,12 @@ impl Handle<GenerationWithDetectionTask> for Orchestrator {
let trace_id = task.trace_id;
info!(%trace_id, config = ?task.detectors, "task started");

// TODO: validate requested guardrails
validate_detectors(
&task.detectors,
&ctx.config.detectors,
&[DetectorType::TextGeneration],
true,
)?;

// Handle generation
let client = ctx
Expand Down
26 changes: 24 additions & 2 deletions src/orchestrator/handlers/streaming_classification_with_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ use tracing::{Instrument, error, info, instrument};
use super::Handle;
use crate::{
clients::GenerationClient,
config::DetectorType,
models::{
ClassifiedGeneratedTextStreamResult, DetectionWarning, DetectorParams, GuardrailsConfig,
GuardrailsHttpRequest, GuardrailsTextGenerationParameters,
TextGenTokenClassificationResults,
},
orchestrator::{
Context, Error, Orchestrator, common,
Context, Error, Orchestrator,
common::{self, validate_detectors},
types::{
Chunk, DetectionBatchStream, DetectionStream, Detections, GenerationStream,
MaxProcessedIndexBatcher,
Expand Down Expand Up @@ -72,7 +74,27 @@ impl Handle<StreamingClassificationWithGenTask> for Orchestrator {
let input_detectors = task.guardrails_config.input_detectors();
let output_detectors = task.guardrails_config.output_detectors();

// TODO: validate requested guardrails
// input detectors validation
if let Err(error) = validate_detectors(
&input_detectors,
&ctx.config.detectors,
&[DetectorType::TextContents],
false,
) {
let _ = response_tx.send(Err(error)).await;
return;
}

// output detectors validation
if let Err(error) = validate_detectors(
&output_detectors,
&ctx.config.detectors,
&[DetectorType::TextContents],
false,
) {
let _ = response_tx.send(Err(error)).await;
return;
}
Comment on lines +77 to +97
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since a user issue led to triggering of an error here for the current version of the orchestrator - why are the whole doc chunkers deliberately disallowed here, especially for both input and output streaming? This would disallow a case of say using Granite Guardian as a content detector.


if !input_detectors.is_empty() {
// Handle input detection
Expand Down
14 changes: 12 additions & 2 deletions src/orchestrator/handlers/streaming_content_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ use tracing::{Instrument, error, info, instrument};

use super::Handle;
use crate::{
config::DetectorType,
models::{DetectorParams, StreamingContentDetectionRequest, StreamingContentDetectionResponse},
orchestrator::{
Context, Error, Orchestrator, common,
Context, Error, Orchestrator,
common::{self, validate_detectors},
types::{BoxStream, DetectionBatchStream, DetectionStream, MaxProcessedIndexBatcher},
},
};
Expand Down Expand Up @@ -65,7 +67,15 @@ impl Handle<StreamingContentDetectionTask> for Orchestrator {
};
info!(%trace_id, config = ?detectors, "task started");

// TODO: validate requested guardrails
if let Err(error) = validate_detectors(
&detectors,
&ctx.config.detectors,
&[DetectorType::TextContents],
false,
) {
let _ = response_tx.send(Err(error)).await;
return;
}

handle_detection(ctx, trace_id, headers, detectors, input_stream, response_tx)
.await;
Expand Down
13 changes: 11 additions & 2 deletions src/orchestrator/handlers/text_content_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ use tracing::{info, instrument};

use super::Handle;
use crate::{
config::DetectorType,
models::{DetectorParams, TextContentDetectionHttpRequest, TextContentDetectionResult},
orchestrator::{Error, Orchestrator, common},
orchestrator::{
Error, Orchestrator,
common::{self, validate_detectors},
},
};

impl Handle<TextContentDetectionTask> for Orchestrator {
Expand All @@ -39,7 +43,12 @@ impl Handle<TextContentDetectionTask> for Orchestrator {
let trace_id = task.trace_id;
info!(%trace_id, config = ?task.detectors, "task started");

// TODO: validate requested guardrails
validate_detectors(
&task.detectors,
&ctx.config.detectors,
&[DetectorType::TextContents],
true,
)?;

// Handle detection
let (_, detections) = common::text_contents_detections(
Expand Down
1 change: 0 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use hyper::Uri;
use url::Url;

pub mod json;
pub mod tls;
pub mod trace;
Expand Down
Loading