Skip to content

upgrade responsible_ai guardrails samples to active models and inference profiles (FIX for #708)#709

Open
rostcheck wants to merge 2 commits into
aws-samples:mainfrom
rostcheck:fix/upgrade-guardrails-samples-to-active-models
Open

upgrade responsible_ai guardrails samples to active models and inference profiles (FIX for #708)#709
rostcheck wants to merge 2 commits into
aws-samples:mainfrom
rostcheck:fix/upgrade-guardrails-samples-to-active-models

Conversation

@rostcheck

Copy link
Copy Markdown
Contributor

Fix for #708:

  • Replace deprecated model IDs with active inference profiles:
    • claude-3-haiku → us.anthropic.claude-haiku-4-5-20251001-v1:0
    • claude-3-sonnet/claude-3-5-sonnet → us.anthropic.claude-sonnet-4-6
    • stability.sd3-large → stability.stable-image-core-v1:1 (us-west-2)
  • Make all guardrail creation calls idempotent (handle ConflictException)
  • Fix apply_guardrail_api notebook: use narrow topic policy so contextual grounding demo works correctly without blocking its own test content
  • Add guard cell to stop Run All before SageMaker-dependent sections
  • Fix langchain import for v1.3+ (langchain_classic.agents)
  • Fix syntax error in Guardrails with LangChain notebook
  • Remove stale Claude 3 text references
  • Remove top_p/top_k params incompatible with newer Anthropic models
  • Fix TDD guardrail notebook: get_denied_topics() now passes existing topics and test failures to the LLM so iterative refinement works
  • Add validate_topics() to enforce Bedrock API length constraints
  • Replace interactive input() calls with defaults for automated execution
  • Regenerate .md and chart PNGs from latest notebook execution
  • Add .gitignore for tdd-guardrail runtime artifacts
  • Update README.md to fix broken "tdd-guardrails" link and include descriptions for all notebooks

Issue #, if available:

Description of changes:

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

…nce profiles

- Replace deprecated model IDs with active inference profiles:
  - claude-3-haiku → us.anthropic.claude-haiku-4-5-20251001-v1:0
  - claude-3-sonnet/claude-3-5-sonnet → us.anthropic.claude-sonnet-4-6
  - stability.sd3-large → stability.stable-image-core-v1:1 (us-west-2)
- Make all guardrail creation calls idempotent (handle ConflictException)
- Fix apply_guardrail_api notebook: use narrow topic policy so contextual
  grounding demo works correctly without blocking its own test content
- Add guard cell to stop Run All before SageMaker-dependent sections
- Fix langchain import for v1.3+ (langchain_classic.agents)
- Fix syntax error in Guardrails with LangChain notebook
- Remove stale Claude 3 text references
- Remove top_p/top_k params incompatible with newer Anthropic models
- Fix TDD guardrail notebook: get_denied_topics() now passes existing
  topics and test failures to the LLM so iterative refinement works
- Add validate_topics() to enforce Bedrock API length constraints
- Replace interactive input() calls with defaults for automated execution
- Regenerate .md and chart PNGs from latest notebook execution
- Add .gitignore for tdd-guardrail runtime artifacts
- Update README.md to fix broken "tdd-guardrails" link and include descriptions for all notebooks
@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@rostcheck

Copy link
Copy Markdown
Contributor Author

Note: all notebooks were manually executed and reviewed.

@athewsey athewsey self-assigned this Jun 8, 2026

@athewsey athewsey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi thanks for working through these updates! A couple of change requests I found while working through, but happy to discuss further - let me know what you think

Comment thread responsible_ai/README.md

- [bedrock-guardrails](./bedrock-guardrails) - Examples highlighting how Amazon Bedrock Guardrails can be used.
- [tdd-guardrail](./tdd-guardrails) - Explore a solution that automates building guardrails using a test-driven development approach.
### [bedrock-guardrails](./bedrock-guardrails)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A bit concerned that this added detail makes the doc more effort to maintain, while still not being complete (what about the 4 other folders?)

Suggest to include entries for the other folders under responsible-ai, and consider whether the value-maintenance trade-off is good for listing every notebook vs giving general folder-level bullets as before.

" if _g['name'] == 'fiduciary-advice':\n",
" print(f\"Deleting existing guardrail: {_g['id']}\")\n",
" bedrock_client.delete_guardrail(guardrailIdentifier=_g['id'])\n",
" import time; time.sleep(2)\n",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not a big fan of the inline import being used in this pattern. I see a lot of these notebooks rely on inline rather than having imports up-front, but up-front should be preferred where we reasonably can?

Comment on lines +157 to +158
" contentPolicyConfig={\n",
" 'filtersConfig': [\n",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Inconsistent indentations here (and propagated further down) don't help readability

},
"outputs": [],
"source": [
"from datetime import datetime\n",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This import is already present in the previous cell, which is necessary to run this one (imports boto3) and should pose no problem to re-run as needed (doesn't change anything apart from re-creating the boto client).

...So suggest we should drop this duplicate import

Comment on lines +122 to +126
"# Create a version\n",
"bedrock.create_guardrail_version(\n",
" guardrailIdentifier=guardrailId,\n",
" description='Version 1'\n",
")\n",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why create a version, if you go ahead and use DRAFT in the LangChains? I'd probably drop this and stick to DRAFT to reduce complexity... but if you really want to teach versioning within this notebook, then could:

  1. Add some explanation, and
  2. Use the created version in the examples later on?

Comment on lines +806 to +807
" contentType=\"application/json\",\n",
" accept=\"application/json\",\n",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does the new model no longer work okay omitting these specifiers? I'd probably keep them omitted as before, unless we have to, because json is the default anyway and used in pretty much every use case.

" images = response_body[\"images\"]\n",
" for image in images:\n",
" image_id = ''.join(random.choices(string.ascii_lowercase + string.digits, k=6))\n",
" image_file = os.path.join(output_images_folder, \"generated-image-{}.jpg\".format(image_id))\n",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like you've also removed the saving of generated images - which I didn't see mentioned in the PR description.

I could be persuaded either way on whether omitting this is good... But if it's deliberate, then we should remove the output_images_folder variable declaration and the unused base64 import too.

Comment on lines +805 to +811
" # Guardrail already exists from a prior run - delete and recreate\n",
" print(f\"Guardrail '{guardrail_name}' already exists. Deleting and recreating.\")\n",
" _existing = client.list_guardrails(maxResults=50)\n",
" for _g in _existing['guardrails']:\n",
" if _g['name'] == guardrail_name:\n",
" client.delete_guardrail(guardrailIdentifier=_g['id'])\n",
" break\n",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since we're iteratively refining, isn't this expected to be an update? And in that context, would it make more sense to call the UpdateGuardrail API in this case rather than deleting and re-creating?

Comment on lines +872 to +893
"import re\n",
"json_text = auto_generated_tests\n",
"# Strip markdown code fences if present\n",
"json_match = re.search(r'```(?:json)?\\s*([\\s\\S]*?)```', json_text)\n",
"if json_match:\n",
" json_text = json_match.group(1).strip()\n",
"\n",
"# Try to parse as JSON array\n",
"try:\n",
" data = json.loads(json_text)\n",
"except json.JSONDecodeError:\n",
" # Try to find a JSON array in the text\n",
" array_match = re.search(r'(\\[\\s*\\{[\\s\\S]*\\}\\s*\\])', json_text)\n",
" if array_match:\n",
" data = json.loads(array_match.group(1))\n",
" else:\n",
" print(\"Warning: Could not parse model output as JSON. Using fallback test data.\")\n",
" data = [\n",
" {\"test_input\": \"Help me with algebra homework for my 8th grader\", \"expected_action\": \"NONE\", \"source\": \"INPUT\"},\n",
" {\"test_input\": \"Can you tutor my kid in person?\", \"expected_action\": \"GUARDRAIL_INTERVENED\", \"source\": \"INPUT\"},\n",
" {\"test_input\": \"Teach me history\", \"expected_action\": \"GUARDRAIL_INTERVENED\", \"source\": \"INPUT\"},\n",
" ]\n",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks very similar to the parsing logic above. Would it make sense to factor into a function defined once (or even hidden in a utility py file) and used twice?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants