Skip to content

Refactor response handling in websocket_wiki.py#459

Merged
sng-asyncfunc merged 2 commits into
AsyncFuncAI:mainfrom
loon-hejw:main
Jan 23, 2026
Merged

Refactor response handling in websocket_wiki.py#459
sng-asyncfunc merged 2 commits into
AsyncFuncAI:mainfrom
loon-hejw:main

Conversation

@loon-hejw
Copy link
Copy Markdown
Contributor

"No valid XML" when using ollama
#390

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @loon-hejw, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the response handling mechanism within the websocket_wiki.py file to improve the parsing of streaming data from the Ollama model. The changes are designed to make the system more resilient to variations in Ollama's response formats, thereby resolving issues related to content extraction and ensuring a smoother user experience when interacting with the model.

Highlights

  • Improved Ollama Response Parsing: The logic for handling streaming responses from Ollama has been refactored to more robustly extract text content. This addresses issues where the previous implementation failed to correctly parse diverse response formats, preventing errors like 'No valid XML'.
  • Enhanced Content Extraction: New checks have been introduced to identify and extract content from various chunk structures, including dictionary-based messages, object attributes, and __dict__ introspection, ensuring reliable text retrieval.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

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 refactors the response handling for Ollama to address an XML parsing issue. The new logic is more defensive and handles various response structures. My review focuses on improving the maintainability of this new, complex logic by suggesting a refactoring into a helper function and simplifying some of the internal expressions for better readability.

Comment thread api/websocket_wiki.py
Comment on lines +581 to +602
text = None
if isinstance(chunk, dict):
text = chunk.get("message", {}).get("content") if isinstance(chunk.get("message"), dict) else chunk.get("message")
else:
message = getattr(chunk, "message", None)
if message is not None:
if isinstance(message, dict):
text = message.get("content")
else:
text = getattr(message, "content", None)

if not text:
text = getattr(chunk, 'response', None) or getattr(chunk, 'text', None)

if not text and hasattr(chunk, "__dict__"):
message = chunk.__dict__.get("message")
if isinstance(message, dict):
text = message.get("content")

if isinstance(text, str) and text and not text.startswith('model=') and not text.startswith('created_at='):
clean_text = text.replace('<think>', '').replace('</think>', '')
await websocket.send_text(clean_text)
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

The logic for extracting text from the response chunk is quite complex and is now spread across many lines within the async for loop. This reduces the readability of the loop's main purpose.

To improve maintainability and separation of concerns, consider refactoring this extraction logic into a separate helper function, for example, _extract_text_from_chunk(chunk).

Within this new function, you could also:

  • Simplify the complex expression on line 583 into a more readable if/else block.
  • Re-evaluate the necessity of the __dict__ access on lines 595-598. Direct __dict__ access is an anti-pattern and might be redundant given the getattr call that precedes it. If it's a necessary workaround, a comment explaining why would be valuable.

Here's an example of how the refactoring could look:

from typing import Any, Optional

def _extract_text_from_chunk(chunk: Any) -> Optional[str]:
    """Extracts text content from a response chunk of unknown structure."""
    text = None
    if isinstance(chunk, dict):
        message = chunk.get("message")
        if isinstance(message, dict):
            text = message.get("content")
        else:
            text = message
    else:
        message = getattr(chunk, "message", None)
        if message is not None:
            if isinstance(message, dict):
                text = message.get("content")
            else:
                text = getattr(message, "content", None)

    if not text:
        text = getattr(chunk, 'response', None) or getattr(chunk, 'text', None)

    # This part is questionable and might be removable.
    if not text and hasattr(chunk, "__dict__"):
        message = chunk.__dict__.get("message")
        if isinstance(message, dict):
            text = message.get("content")
            
    return text

async def handle_websocket_chat(websocket: WebSocket):
    # ...
    async for chunk in response:
        text = _extract_text_from_chunk(chunk)
        if isinstance(text, str) and text and not text.startswith('model=') and not text.startswith('created_at='):
            clean_text = text.replace('<think>', '').replace('</think>', '')
            await websocket.send_text(clean_text)
    # ...

@sng-asyncfunc sng-asyncfunc merged commit 73ace1a into AsyncFuncAI:main Jan 23, 2026
afanty2021 added a commit to afanty2021/deepwiki-open that referenced this pull request Feb 10, 2026
- Add multi-language README support (10 languages)
- Add Bedrock model support (PR AsyncFuncAI#420)
- Add embedding diagnostics improvements (PR AsyncFuncAI#423)
- Add websocket_wiki.py refactoring (PR AsyncFuncAI#459)
- Add README screenshot emoji cleanup (PR AsyncFuncAI#449)
- Update current documentation timestamp
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