- Receives a POST request with the current request state
- Processes the data according to your custom logic
- Returns a JSON response indicating whether to allow, reject, or modify the request
Request Schema
When a plugin is invoked, the gateway sends a POST request with the following JSON payload:Input Structure
Field Descriptions
Field | Type | Description |
---|---|---|
messages | Array<Message> | The messages array being processed. In REQUEST phase, this is what will be sent to the LLM. In RESPONSE phase, this includes the LLM’s response as the last message. |
requestBody | object | The complete request body from the client, including all LLM parameters (model, temperature, etc.) |
requestHeaders | object | All HTTP headers from the client’s request |
metadata | object | Custom metadata from the request. Can include prompt info, model provider details, etc. |
configs | any | The custom configuration object you defined for this plugin instance in the endpoint configuration |
requestId | string | A unique UUID identifying this specific request, useful for logging and correlation |
Phase-Specific Considerations
REQUEST Phase:messages
contains the messages that will be sent to the LLM (after prompt template application)requestBody
contains the original client request parametersmetadata
may include prompt information if a prompt template was used
messages
contains all input messages PLUS the LLM’s response message as the last elementrequestBody.response
contains the full LLM response objectmetadata
includes additional fields likemodelProvider
andpromptInfo
- Same as RESPONSE phase
- Intended for non-blocking analytics, monitoring, or logging operations
Response Schema
Your plugin must return a JSON response matching this schema:Output Structure
Field Descriptions
Field | Type | Required | Default | Description |
---|---|---|---|---|
reject | boolean | No | false | Set to true to reject the request/response and stop pipeline execution |
rejectReason | string | No | - | Human-readable explanation for why the request was rejected. Returned to the client. |
dontRetry | boolean | No | false | Prevents retries with other LLMs (only in RESPONSE phase). If true , the gateway will not attempt to call alternative models if the response is rejected. |
messages | Array<Message> | No | - | Modified messages array. If provided, these messages replace the input messages for subsequent plugins. |
debug | Array<string> | No | - | Debug messages that will be included in the gateway logs for troubleshooting |
Response Validation
The gateway validates your plugin’s response against the schema above using Zod. If validation fails:- The plugin execution is marked as failed
- The error is logged with the validation error message
- The request continues (fail-open behavior) with the original, unmodified messages
Implementation Examples
Example 1: Simple Content Filter (Guardrail)
This plugin rejects requests containing prohibited words.Example 2: PII Redaction (Modification)
This plugin detects and redacts personally identifiable information.Example 3: Response Quality Check (Guardrail)
This plugin validates that LLM responses meet quality standards.Example 4: Context Enhancement (Modification)
This plugin adds additional context to user requests.Testing Your Plugin
Local Testing
Before deploying your plugin, test it locally using curl or any HTTP client:Integration Testing
Once your plugin is deployed and configured in the gateway:- Monitor the gateway logs for plugin execution messages
- Check the
debug
array in your responses - these will appear in gateway logs - Use the gateway’s inference logs to see plugin execution times and results
- Test timeout and retry behavior by simulating slow responses or failures
Best Practices
Performance
Response Time:- Aim for plugin response times under 100ms for REQUEST phase
- RESPONSE phase plugins can be slightly slower (under 500ms)
- Use LOG phase for any operations that can be async (analytics, slow external APIs)
- Cache frequently used data (e.g., ML models, lookup tables)
- Use connection pooling for database queries
- Implement circuit breakers for external API calls
- Consider using async/parallel processing internally
Error Handling
Fail Gracefully:- Set appropriate timeout values in your plugin configuration
- Ensure your plugin respects the timeout and fails fast
- Use async operations to avoid blocking
Security
As your plugins will be exposed over the internet, it’s crucial to implement robust security measures. We recommend checking for a secret header, which you can configure in the Datawizz dashboard when setting up your plugin endpoint. Authentication:- Always validate the structure of incoming requests
- Sanitize any data before using it in queries or external API calls
- Be cautious with the
configs
field - validate expected types
- Never hardcode API keys or secrets in your plugin code
- Use environment variables or secret management services
- Rotate credentials regularly
Observability
Logging:- Track plugin execution time
- Monitor rejection rates
- Alert on error rates
- Track resource usage (CPU, memory, network)
Message Handling
Preserve Message Structure:Troubleshooting
Common Issues
“Plugin response validation failed”- Check that your response matches the expected schema exactly
- Ensure
reject
is a boolean, not a string - Ensure
debug
is an array of strings, not a single string - Verify
messages
is an array if provided
- Your plugin is taking longer than the configured timeout
- Optimize your plugin’s processing time
- Increase the timeout value in plugin configuration
- Move slow operations to LOG phase if possible
- Your plugin threw an unhandled exception
- Check your plugin’s logs for error details
- Implement proper error handling
- Ensure you’re returning a
messages
array in your response - Verify the array contains valid message objects
- Check that you’re not accidentally returning the original messages reference
- Ensure
reject: true
is present in response - Include a
rejectReason
string - Check that the response is valid JSON
Next Steps
Now that you understand how to build plugins:- Implement a simple plugin following one of the examples above
- Test it locally with sample requests
- Deploy it to your hosting platform
- Configure it in the Datawizz dashboard
- Monitor its performance and iterate
- Plugin Usage Guide - How to use and configure plugins