Add guide for writing custom Python policies#1931
Conversation
Introduce a new step-by-step guide (docs/gateway/policies/writing-custom-python-policies.md) that explains how to author, package, and build Python policies for the gateway, including examples, SDK usage, directory layout, requirements handling, build/register instructions, best practices, and troubleshooting. Update docs/gateway/README.md and docs/gateway/policy-languages-and-runtimes.md to link to the new guide and surface it in the next-steps/navigation.
📝 WalkthroughSummaryAdded comprehensive documentation for authoring custom Python policies for the API Platform Gateway, addressing issue ChangesNew Guide:
Updated:
Updated:
ImpactEnables users to implement custom Python gateway policies by providing actionable documentation with examples, schema definitions, and best practices. Improves discoverability through updated documentation index and navigation links. WalkthroughThis pull request adds a complete documentation guide for writing custom Python policies in the API Platform Gateway. The changes include a new comprehensive guide page covering prerequisites, policy directory structure, step-by-step implementation instructions (policy-definition.yaml schema, policy.py code structure, dependencies, and registration), reference tables for context objects and return types, an end-to-end example, best practices, and troubleshooting. Navigation links are added to existing documentation pages to surface this new resource. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
docs/gateway/policies/writing-custom-python-policies.md (2)
472-488: 💤 Low valueConsider adding type hints to the
get_policy()signature for consistency.Throughout the guide, policy hook methods use explicit type hints (e.g.,
params: dict[str, Any]). For consistency and to reinforce best practices, consider adding type hints to theget_policy()factory function signature.♻️ Suggested enhancement
-def get_policy(metadata, params): +def get_policy(metadata, params: dict[str, Any]): """Factory function — called by the Python executor to create the policy.Note: If the
metadatatype is available in the SDK, include it as well.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@docs/gateway/policies/writing-custom-python-policies.md` around lines 472 - 488, The get_policy factory function lacks type hints; update the signature of get_policy to include types for metadata and params and a return type (e.g., def get_policy(metadata: PolicyMetadata, params: dict[str, Any]) -> MyPolicy:) using the SDK's PolicyMetadata type if available and the concrete policy class (MyPolicy) as the return type so it matches the other annotated hooks and reinforces best practices.
24-29: 💤 Low valueConsider adding a language specifier to the code fence.
For better rendering and accessibility, the directory structure code block should specify a language identifier (e.g.,
textorbash).📝 Suggested fix
-``` +```text my-policy/ ├── policy-definition.yaml # Identity, version, and parameter schema ├── policy.py # Policy implementation (entry point)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@docs/gateway/policies/writing-custom-python-policies.md` around lines 24 - 29, Update the fenced directory-structure block that starts with "my-policy/" in writing-custom-python-policies.md to include a language specifier (e.g., change the opening ``` to ```text) so the tree renders properly; keep the content unchanged and only modify the opening fence for the code block that contains the "my-policy/" directory listing.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@docs/gateway/policies/writing-custom-python-policies.md`:
- Around line 472-488: The get_policy factory function lacks type hints; update
the signature of get_policy to include types for metadata and params and a
return type (e.g., def get_policy(metadata: PolicyMetadata, params: dict[str,
Any]) -> MyPolicy:) using the SDK's PolicyMetadata type if available and the
concrete policy class (MyPolicy) as the return type so it matches the other
annotated hooks and reinforces best practices.
- Around line 24-29: Update the fenced directory-structure block that starts
with "my-policy/" in writing-custom-python-policies.md to include a language
specifier (e.g., change the opening ``` to ```text) so the tree renders
properly; keep the content unchanged and only modify the opening fence for the
code block that contains the "my-policy/" directory listing.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 8f91856b-491f-4769-9ec1-2d27483b5552
📒 Files selected for processing (3)
docs/gateway/README.mddocs/gateway/policies/writing-custom-python-policies.mddocs/gateway/policy-languages-and-runtimes.md
Fix #1705
Purpose
Introduce a new step-by-step guide (docs/gateway/policies/writing-custom-python-policies.md) that explains how to author, package, and build Python policies for the gateway, including examples, SDK usage, directory layout, requirements handling, build/register instructions, best practices, and troubleshooting. Update docs/gateway/README.md and docs/gateway/policy-languages-and-runtimes.md to link to the new guide and surface it in the next-steps/navigation.