Skip to content

Feature/add teacher image input - #368

Merged
Nuh0804 merged 9 commits into
developmentfrom
feature/add-teacher-image-input
Nov 23, 2026
Merged

Nuh0804 merged 9 commits into
developmentfrom
feature/add-teacher-image-input

Conversation

@joelmaharena

Copy link
Copy Markdown
Collaborator

No description provided.

@joelmaharena joelmaharena linked an issue Oct 29, 2026 that may be closed by this pull request
@Hectorfr
Hectorfr requested a review from Nuh0804 October 29, 2026 16:45
@Nuh0804

Nuh0804 commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Great Job on this!

I ran this branch, merged with the latest development, against a real WhatsApp number. Photo transcription itself works well, but two behaviours need fixing before merge.

Test setup: app in Docker (docker/dev), vision model qwen2.5vl:7b on local Ollama, chat model MiniMax-M3 via Together, a teacher account for Geography Form 2.

What happened on the phone

Sent Result
File sent as a document Treated as unsupported. The reply says "please send your question as text"
Photo, caption "answer question 1" Transcribed correctly (10.3 s, model loading). The agent answered both questions and did not search the textbook
Photo, then the text "answer question 1 only" 4 s later The text was answered first, from the previous photo in history. The new photo was then answered separately: two replies
Photo, caption "answer question 4 only" ✅ Answered only Q4 (no textbook search)
Photo, then the text "answer question 1 only" 3 s later Same race again: duplicate reply, the first based on the previous photo

What works: all photos were transcribed accurately and rated clear. Vision took 5.4–10.3 s, about 7–12 s including the download, and the full reply took 10–16 s. When the caption says which question to answer, it's followed. Meta's webhook resends were dropped by the 10-second OUTDATED check, so no duplicates came from them.


1. Critical issues

  • A photo followed by a text message gives two answers, and the first can be about the wrong photo.
    A text message skips the image step, so it reaches agent_client while the photo is still being downloaded and read (2–8 s) and gets answered from whatever image is already in history. The photo is then answered on its own. Teachers will naturally send a photo and then type "answer question 2", so this will happen often.
    Suggested fix: have a user's text message wait while that user's photo is still being read, or put the photo message into the agent's queue before interpretation so the text waits behind it.

  • Answers to photo questions skip the textbook and can contradict it.
    None of the photo replies called search_knowledge, while typed questions in the same session did and cited Sources: [1] Geography … Form Two. One example: for "What type of vegetation grows in Dodoma?" the photo answer said "savanna grassland", but the textbook passage retrieved for the same question says the vegetation is "mainly shrubs".
    Likely cause: the text ImageInterpretation.to_request_text builds starts with Source: teacher image … Transcription: …, which reads like source material that's already been provided.
    Suggested fix: word it as the teacher's request (e.g. "The teacher sent a photo of these questions: …"), add a line to the agent prompt saying that questions from photos must also be checked against the textbook, and add an evaluation case for it.

2. Performance & edge cases

  • Failed photos leave their placeholder in the chat history. On failure, the message stays as Image not yet interpreted. with is_present_in_conversation=True. After a few failures the agent started copying them ("I still can't see the image…") even once photos were working. Hide the placeholder from history when processing fails.
  • Failures aren't diagnosable from the logs. A vision failure logs only Image stage=vision result=failed elapsed_ms=23. The real cause (connection refused to the vision endpoint from inside Docker) needed a docker exec to find. Log the exception type at the download and vision stages, without the request or response contents.
  • Photos sent as documents get misleading advice. type: document with an image MIME type gets "please send your question as text". Either accept JPEG/PNG documents, or change unsupported_message to suggest sending it as a photo.
  • Captions are only partly followed. "answer question 1" got both questions answered; "answer question 4 only" worked. This depends on the model, but a clearer instruction would help.

Recommendation: request changes. Photo reading itself works well. The duplicate or wrong-photo answers and the answers that skip the textbook should be fixed before merge; both appeared within 10 minutes of real-phone testing.

@Nuh0804 Nuh0804 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Following up on my first review with a second round of testing: this branch merged into the latest development, vision on Together with zai-org/GLM-5.3-Flash.

Photo reading on a hosted model works well. A photo with three questions was read in 1.7 s and rated clear. The branch hasn't changed since the first review, so those points are still open. Syncing with development also needs a few changes that won't show up as conflicts.

0. Sync with development (required)

Textual conflicts in .env.template, .env.template.simple, app/assets/strings/english.yml and app/services/messaging_service.py. Beyond those:

  • llm_client and agentic_mode are gone from development. Every chat now goes through agent_client. Remove _get_llm_client() and the llm_client parameter of _generate_and_deliver, and call agent_client directly. Keep development's _save_profile_name_reply check at the top of handle_chat_message.
  • The prompt registry now requires path:. Without it, the app crashes on startup with KeyError: 'path' (inline).
  • test_image_extraction_reaches_agent_and_existing_delivery patches llm_settings.agentic_mode and llm_client. Remove those two patches and basic.assert_not_awaited().
  • english.yml: keep development's new help text ("change your name or your classes and subjects") and add image_help below it.
  • .env templates: keep both development's model-fallback block and your vision block.

With these changes the full test suite passed for me. Heads-up: #363 (voice notes) also changes is_other_message and WhatsAppClient, so whichever lands second needs a small merge there.

1. Still open from the first review

  • Photo followed by text gives two replies (critical). I didn't trigger it this round, but the code is unchanged (inline).
  • Failed photos leave their placeholder in history (inline).
  • Photos sent as documents get "send your question as text". Seen again this round (inline).
  • Request text reads like source material. Mostly fixed by #380 in my test, since photo answers searched the textbook. Still worth rewording, now lower priority (inline).

2. Vision failures hide their cause (medium)

With a wrong model ID, every photo failed in about 0.5 s, and the only log line was Image stage=vision result=failed model=GLM-5.3-Flash. The provider's 404 is caught and dropped. Finding it meant going through config by hand (inline).

3. A bad vision URL takes the whole bot down (medium)

With IMAGE_INPUT_ENABLED=True and a local Ollama URL (http://host.docker.internal:11434/v1), the app crashed at import with VISION_BASE_URL must use HTTPS, so text chat went down too (inline).

Comment thread app/services/messaging_service.py Resolved
Comment thread app/services/messaging_service.py Resolved
Comment thread app/services/messaging_service.py Resolved
Comment thread app/clients/image_interpreter.py Resolved
Comment thread app/config.py
Comment on lines +80 to +81
if not self.vision_base_url.startswith("https://"):
raise ValueError("VISION_BASE_URL must use HTTPS")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

With local Ollama (http://host.docker.internal:11434/v1, the setup used for testing) the app now crashes at import, which takes text chat down as well. Suggest allowing plain http only for local hosts:

url = urlsplit(self.vision_base_url)
local = url.hostname in ("localhost", "127.0.0.1", "::1", "host.docker.internal")
if url.scheme != "https" and not (url.scheme == "http" and local):
    raise ValueError("VISION_BASE_URL must use HTTPS unless it is local")

Also consider logging the error and turning image input off instead of failing startup, so a vision misconfiguration can't stop the whole bot.

Comment on lines 375 to +380
def is_other_message(message_info: dict) -> bool:
return message_info.get("message", {}).get("type") not in ["text", "interactive"]
return message_info.get("message", {}).get("type") not in [
"text",
"interactive",
"image",
]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Still open: photos sent as documents. A JPEG sent as a file is type: document and gets "please send your question as text", which is the wrong advice. Either route document messages with an image MIME type (image/jpeg, image/png) through the image path, or change unsupported_message to suggest resending it as a photo.

Comment on lines +40 to +46
def to_request_text(self, caption: str) -> str:
return (
"Source: teacher image\n"
f"Teacher instruction: {caption.strip() or 'none'}\n"
f"Transcription: {self.transcription.strip() or 'none'}\n"
f"Relevant visual details: {self.visual_details.strip() or 'none'}"
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Lower priority now: with #380's prompt, photo answers searched the textbook in my test. Wording this as the teacher's request instead of as a source would still make it more robust, for example:

The teacher sent a photo with this request: {caption or 'none'}
Text in the photo: ...
Visual details: ...

Comment on lines +18 to +19
image_interpreter_system:
active_version: v0.0

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

development's prompt registry now requires a path for every entry, so the app crashes on startup with KeyError: 'path' without it:

  image_interpreter_system:
    path: image_interpreter_system
    active_version: v0.0

Keep both the model-fallback and vision blocks in the env templates,
and restore the blank line black expects in logging_utils.
A text sent while a photo was being read took the agent turn first and
was answered against the previous photo. The agent now accepts a
`prepare` step that runs while holding the turn, so texts arriving
meanwhile wait and are answered together with the interpreted photo.

- A failed photo is dropped from the batch; waiting texts are still
  answered, after the image error is sent.
- A photo arriving during another turn is read before it is buffered,
  so the running turn never answers it unread.
- Add MessageProcessor.remove (identity match) and PreparationFailed.
A failed photo kept its "Image not yet interpreted." placeholder in the
conversation, so the agent later said it still could not see the image.
On any image error the row is now marked "[photo could not be read]" and
left out of the agent's history; it stays in the database for audit.

Generalise db.update_message_content into update_message, which updates
only the given fields and keeps the owner guard and single-row check.
_cleanup_processor ran inside the locked block, so the lock was always
held and no processor was ever removed: one stayed in memory per user
until restart. Run the cleanup after the lock is released.
@Nuh0804
Nuh0804 marked this pull request as ready for review November 22, 2026 21:53

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Your trial has ended. Reactivate Greptile to resume code reviews.

An earlier merge of development kept this branch's older help text,
so merging the PR would have reverted it in development.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Your trial has ended. Reactivate Greptile to resume code reviews.

@Nuh0804
Nuh0804 merged commit fc2558d into development Nov 23, 2026
5 checks passed
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.

Image-to-Action Pipeline

2 participants