medical agent fix#482
Conversation
📝 WalkthroughWalkthroughThe PR reorganizes the medical agent example by restructuring the implementation from Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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.
Actionable comments posted: 5
🧹 Nitpick comments (2)
examples/medical_agent/medical_agent.py (2)
20-29: Moveload_dotenv()call after imports, or keep imports at top of file (PEP 8).Per PEP 8, all imports should appear at the top of the module.
load_dotenv()only needs to run beforeos.getenv("OPENROUTER_API_KEY")is evaluated (line 52), not before theagno/binduimports. Reordering will also likely resolve the pre-commit trailing-whitespace / formatting pipeline failure cleanly.♻️ Proposed reordering
import os from dotenv import load_dotenv - -# Load environment variables from .env file -load_dotenv() from bindu.penguin.bindufy import bindufy from agno.agent import Agent from agno.tools.duckduckgo import DuckDuckGoTools from agno.models.openrouter import OpenRouter + +# Load environment variables from .env file +load_dotenv()🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@examples/medical_agent/medical_agent.py` around lines 20 - 29, The call to load_dotenv() is currently before module imports, violating PEP 8; move the load_dotenv() invocation so all imports (e.g., from bindu.penguin.bindufy import bindufy, from agno.agent import Agent, from agno.tools.duckduckgo import DuckDuckGoTools, from agno.models.openrouter import OpenRouter) remain at the top of the file, then call load_dotenv() afterwards but before any use of environment variables (specifically before os.getenv("OPENROUTER_API_KEY") / OpenRouter instantiation) so env vars are available when needed.
72-97: Handler edge cases and response extraction.A few small robustness points:
- Line 84:
messages[-1].get('content', '')may yield an empty string, which is then passed toagent.run(input=""). Consider falling back to the prompt message (line 97) whencontentis empty/whitespace, instead of only whenmessagesis empty.- Lines 90–95: checking
hasattr(result, 'content')and then'response'is a fragile duck-typing ladder. If the AgnoRunResponseAPI is known, access.contentdirectly (or viaresult.to_dict()["content"]as shown in thebindufydocstring example) rather than falling through tostr(result), which can leak internal repr.- Add a return type annotation (
-> str) for clarity and to match the docstring contract.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@examples/medical_agent/medical_agent.py` around lines 72 - 97, The handler function should declare a return type (-> str), treat empty/whitespace latest message as missing by falling back to the default prompt, and extract the agent output in a robust way instead of fragile hasattr checks; update handler to compute latest_message by trimming messages[-1].get('content','') and if that is empty use the fallback prompt, call agent.run(input=latest_message), then extract the response from the known RunResponse shape (access .content directly or use result.to_dict()["content"] if a dict form is provided) and return that string; avoid using hasattr checks and str(result) to prevent leaking internal repr.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@examples/medical_agent/medical_agent.py`:
- Around line 34-49: Remove trailing whitespace characters from the multi-line
string assigned to the variable instructions in medical_agent.py (the line
ending after "disclaimers." and any other lines in that string); run the
pre-commit hooks locally (pre-commit run --all-files) to verify and let the
hooks auto-fix remaining trailing-whitespace issues, then stage and commit the
updated file so CI no longer fails.
- Around line 50-53: Before constructing the OpenRouter model, explicitly check
that os.getenv("OPENROUTER_API_KEY") returns a non-empty value and raise a clear
error if it's missing; replace the inline call in the OpenRouter(...)
instantiation (the model=OpenRouter(...) block) with a validated variable (e.g.,
openrouter_api_key) and if it's None or empty raise a RuntimeError or ValueError
with a message instructing the user to set OPENROUTER_API_KEY (and reference the
README/.env), so the application fails fast with a helpful message instead of
surfacing a cryptic downstream client error.
In `@examples/medical_agent/README.md`:
- Around line 123-128: Update the troubleshooting snippet to use the same API
key placeholder as .env.example: replace the `OPENROUTER_API_KEY=your_key_here`
instruction with `OPENROUTER_API_KEY=sk-or-v1-<your-api-key>` (or alternatively
change .env.example to use `your_key_here` if you prefer that style); ensure the
README and .env.example use the identical placeholder string so references to
OPENROUTER_API_KEY are consistent across docs.
- Around line 68-73: Add missing fenced-code block language identifiers for the
two Markdown code blocks; update the block containing the "Flu Symptoms
Overview" example to use a language tag like ```markdown (or ```text) and update
the directory-tree/code block later (the one around lines 98–110) to use ```text
so markdownlint MD040 is satisfied—locate the fenced blocks that wrap the "Flu
Symptoms Overview" paragraph and the directory tree and prepend the appropriate
language specifier after the opening backticks.
In `@examples/medical_agent/skills/medical-research-skill/skill.yaml`:
- Around line 28-38: The declared input/output MIME types in skill.yaml
(input_modes/output_modes) don't match actual usage; update input_modes to
"text/plain" (since the handler in medical_agent.py extracts request.content as
a string) and update output_modes to "text/markdown" (or "text/plain" if you
prefer plain text) so the skill I/O contract reflects the handler's
string/markdown responses; also verify the handler code that extracts content
and returns the response (in medical_agent.py) aligns with the chosen output
MIME type.
---
Nitpick comments:
In `@examples/medical_agent/medical_agent.py`:
- Around line 20-29: The call to load_dotenv() is currently before module
imports, violating PEP 8; move the load_dotenv() invocation so all imports
(e.g., from bindu.penguin.bindufy import bindufy, from agno.agent import Agent,
from agno.tools.duckduckgo import DuckDuckGoTools, from agno.models.openrouter
import OpenRouter) remain at the top of the file, then call load_dotenv()
afterwards but before any use of environment variables (specifically before
os.getenv("OPENROUTER_API_KEY") / OpenRouter instantiation) so env vars are
available when needed.
- Around line 72-97: The handler function should declare a return type (-> str),
treat empty/whitespace latest message as missing by falling back to the default
prompt, and extract the agent output in a robust way instead of fragile hasattr
checks; update handler to compute latest_message by trimming
messages[-1].get('content','') and if that is empty use the fallback prompt,
call agent.run(input=latest_message), then extract the response from the known
RunResponse shape (access .content directly or use result.to_dict()["content"]
if a dict form is provided) and return that string; avoid using hasattr checks
and str(result) to prevent leaking internal repr.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 116fd29a-703f-44fe-bf12-06cb934f95dc
📒 Files selected for processing (6)
examples/ai-data-analysis-agent/skills/ai-data-analysis-agent/skill.yamlexamples/medical_agent/.env.exampleexamples/medical_agent/README.mdexamples/medical_agent/agent.pyexamples/medical_agent/medical_agent.pyexamples/medical_agent/skills/medical-research-skill/skill.yaml
💤 Files with no reviewable changes (1)
- examples/medical_agent/agent.py
| instructions="""You are a medical research assistant. When asked about health or medical topics, provide clear, accurate information with appropriate disclaimers. | ||
|
|
||
| Key guidelines: | ||
| - Always include a medical disclaimer stating this is not professional medical advice | ||
| - Provide general health information and educational content | ||
| - For specific medical concerns, recommend consulting healthcare professionals | ||
| - Use web search to find current, reliable medical information | ||
| - Present information in an organized, easy-to-read format | ||
| - Avoid making definitive diagnoses or treatment recommendations | ||
| - Focus on evidence-based information from reputable sources | ||
|
|
||
| Response format: | ||
| - Start with relevant medical information | ||
| - Include supporting details and context | ||
| - End with a clear medical disclaimer | ||
| - Avoid showing multiple search results - synthesize information coherently""", |
There was a problem hiding this comment.
Pre-commit trailing-whitespace failure.
CI reports trailing-whitespace hook modified this file. Line 34 ends with a trailing space after disclaimers. (and possibly others). Please run pre-commit run --all-files locally and commit the fixes so CI passes.
🧰 Tools
🪛 GitHub Actions: CI
[error] pre-commit trailing-whitespace hook failed (exit code 1): files were modified by this hook (Fixing examples/medical_agent/medical_agent.py).
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@examples/medical_agent/medical_agent.py` around lines 34 - 49, Remove
trailing whitespace characters from the multi-line string assigned to the
variable instructions in medical_agent.py (the line ending after "disclaimers."
and any other lines in that string); run the pre-commit hooks locally
(pre-commit run --all-files) to verify and let the hooks auto-fix remaining
trailing-whitespace issues, then stage and commit the updated file so CI no
longer fails.
| model=OpenRouter( | ||
| id="google/gemini-2.0-flash-001", | ||
| api_key=os.getenv("OPENROUTER_API_KEY") | ||
| ), |
There was a problem hiding this comment.
Validate OPENROUTER_API_KEY is set before constructing the model.
os.getenv("OPENROUTER_API_KEY") silently returns None if the variable is missing, which will surface as a confusing downstream error from the OpenRouter client at request time rather than at startup. Fail fast with a clear message so users know to populate .env (the README's troubleshooting section already references this exact error).
🛡️ Proposed fix
+api_key = os.getenv("OPENROUTER_API_KEY")
+if not api_key:
+ raise RuntimeError(
+ "OPENROUTER_API_KEY is not set. Copy .env.example to .env and set your key."
+ )
+
agent = Agent(
...
model=OpenRouter(
id="google/gemini-2.0-flash-001",
- api_key=os.getenv("OPENROUTER_API_KEY")
+ api_key=api_key,
),🧰 Tools
🪛 GitHub Actions: CI
[error] pre-commit trailing-whitespace hook failed (exit code 1): files were modified by this hook (Fixing examples/medical_agent/medical_agent.py).
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@examples/medical_agent/medical_agent.py` around lines 50 - 53, Before
constructing the OpenRouter model, explicitly check that
os.getenv("OPENROUTER_API_KEY") returns a non-empty value and raise a clear
error if it's missing; replace the inline call in the OpenRouter(...)
instantiation (the model=OpenRouter(...) block) with a validated variable (e.g.,
openrouter_api_key) and if it's None or empty raise a RuntimeError or ValueError
with a message instructing the user to set OPENROUTER_API_KEY (and reference the
README/.env), so the application fails fast with a helpful message instead of
surfacing a cryptic downstream client error.
| ``` | ||
| **Flu Symptoms Overview** | ||
| - Common symptoms: Fever, cough, sore throat, body aches | ||
| - Less common: Headache, fatigue, vomiting, diarrhea | ||
| - **Important**: This information is for educational purposes only. Always consult a healthcare professional for medical advice, diagnosis, or treatment. | ||
| ``` |
There was a problem hiding this comment.
Add language identifiers to fenced code blocks (MD040).
markdownlint flags lines 68 and 98 for missing language specifiers. Use ```text (or ```markdown for the response example at 68, and ```text for the directory tree at 98).
As per static analysis hints: MD040, fenced-code-language.
Also applies to: 98-110
🧰 Tools
🪛 markdownlint-cli2 (0.22.0)
[warning] 68-68: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@examples/medical_agent/README.md` around lines 68 - 73, Add missing
fenced-code block language identifiers for the two Markdown code blocks; update
the block containing the "Flu Symptoms Overview" example to use a language tag
like ```markdown (or ```text) and update the directory-tree/code block later
(the one around lines 98–110) to use ```text so markdownlint MD040 is
satisfied—locate the fenced blocks that wrap the "Flu Symptoms Overview"
paragraph and the directory tree and prepend the appropriate language specifier
after the opening backticks.
| #### API Key Not Found | ||
| **Error**: `OPENROUTER_API_KEY not set` | ||
| **Solution**: | ||
| 1. Copy your OpenRouter API key | ||
| 2. Add to `.env` file: `OPENROUTER_API_KEY=your_key_here` | ||
| 3. Restart the agent |
There was a problem hiding this comment.
Troubleshooting snippet is inconsistent with .env.example.
.env.example now uses the placeholder sk-or-v1-<your-api-key>, but this section instructs users to set OPENROUTER_API_KEY=your_key_here. Align the two so users see one consistent format.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@examples/medical_agent/README.md` around lines 123 - 128, Update the
troubleshooting snippet to use the same API key placeholder as .env.example:
replace the `OPENROUTER_API_KEY=your_key_here` instruction with
`OPENROUTER_API_KEY=sk-or-v1-<your-api-key>` (or alternatively change
.env.example to use `your_key_here` if you prefer that style); ensure the README
and .env.example use the identical placeholder string so references to
OPENROUTER_API_KEY are consistent across docs.
| input_modes: | ||
| - application/json | ||
| output_modes: | ||
| - application/json | ||
| examples: | ||
| - "What are the symptoms of flu?" | ||
| - "Provide information about ibuprofen side effects" | ||
| - "How to prevent common cold?" | ||
| - "What are the benefits of regular exercise?" | ||
| - "Explain the symptoms of diabetes" | ||
| - "Give tips for better sleep hygiene" |
There was a problem hiding this comment.
Input/output modes may not match actual usage.
input_modes/output_modes are declared as application/json, but the example prompts are plain-text strings and the handler in medical_agent.py extracts content as a string and returns a string/markdown response. Consider using text/plain (or text/markdown for output) to accurately describe the skill's I/O contract.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@examples/medical_agent/skills/medical-research-skill/skill.yaml` around lines
28 - 38, The declared input/output MIME types in skill.yaml
(input_modes/output_modes) don't match actual usage; update input_modes to
"text/plain" (since the handler in medical_agent.py extracts request.content as
a string) and update output_modes to "text/markdown" (or "text/plain" if you
prefer plain text) so the skill I/O contract reflects the handler's
string/markdown responses; also verify the handler code that extracts content
and returns the response (in medical_agent.py) aligns with the chosen output
MIME type.
Summary by CodeRabbit
Release Notes
New Features
Documentation