Skip to content

fix(group_chat_context): use .get() to avoid KeyError when config profile lacks image_caption_prompt#9216

Open
tsaitang404 wants to merge 0 commit into
AstrBotDevs:masterfrom
tsaitang404:master
Open

fix(group_chat_context): use .get() to avoid KeyError when config profile lacks image_caption_prompt#9216
tsaitang404 wants to merge 0 commit into
AstrBotDevs:masterfrom
tsaitang404:master

Conversation

@tsaitang404

@tsaitang404 tsaitang404 commented Jul 12, 2026

Copy link
Copy Markdown

Description

When group_chat_context.py accesses the config via cfg["provider_settings"]["image_caption_prompt"], it raises KeyError if the config profile was created before this field was added.

Root Cause

group_chat_context.py:59 uses direct dict indexing:

image_caption_prompt = cfg["provider_settings"]["image_caption_prompt"]

cfg comes from get_config(umo=...) which may return an older profile without this key.

Fix

Use .get() with default fallback:

image_caption_prompt = cfg.get("provider_settings", {}).get("image_caption_prompt", "Please describe the image using Chinese.")

Summary by Sourcery

Bug Fixes:

  • Avoid KeyError when loading configs that lack the image_caption_prompt by using a safe lookup with a default prompt.

@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Jul 12, 2026

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request modifies group_chat_context.py to safely retrieve image_caption_prompt from the configuration using .get() with a default value. The reviewer identified a potential AttributeError if provider_settings is explicitly set to None and provided a safer alternative using (cfg.get("provider_settings") or {}) to handle this case.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

cfg = self.context.get_config(umo=event.unified_msg_origin)
group_context_cfg = cfg["provider_ltm_settings"]
image_caption_prompt = cfg["provider_settings"]["image_caption_prompt"]
image_caption_prompt = cfg.get("provider_settings", {}).get("image_caption_prompt", "Please describe the image using Chinese.")

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.

medium

If provider_settings is explicitly configured as null (or parsed as None), cfg.get("provider_settings", {}) will return None instead of {}. Calling .get() on None will then raise an AttributeError. Using (cfg.get("provider_settings") or {}) is safer and prevents potential runtime crashes.

Suggested change
image_caption_prompt = cfg.get("provider_settings", {}).get("image_caption_prompt", "Please describe the image using Chinese.")
image_caption_prompt = (cfg.get("provider_settings") or {}).get("image_caption_prompt", "Please describe the image using Chinese.")

@sourcery-ai sourcery-ai Bot 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.

Hey - I've left some high level feedback:

  • You still access cfg["provider_ltm_settings"] with direct indexing; if older configs can also miss this key, consider using .get() or validating the structure before use to avoid similar KeyErrors.
  • The hard-coded fallback prompt "Please describe the image using Chinese." may not be appropriate for all deployments; consider pulling this default from a shared constant or configuration so language and phrasing can be adjusted centrally.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- You still access `cfg["provider_ltm_settings"]` with direct indexing; if older configs can also miss this key, consider using `.get()` or validating the structure before use to avoid similar `KeyError`s.
- The hard-coded fallback prompt "Please describe the image using Chinese." may not be appropriate for all deployments; consider pulling this default from a shared constant or configuration so language and phrasing can be adjusted centrally.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
astrbot-docs d0e5e68 Jul 12 2026, 08:24 AM

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

Labels

size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant