Skip to content

refactor: extract request body for gemini_model.py. - #899

Merged
mpfaffenberger merged 4 commits into
mpfaffenberger:mainfrom
StarsExpress:refactor-build-request-body
Sep 10, 2026
Merged

mpfaffenberger merged 4 commits into
mpfaffenberger:mainfrom
StarsExpress:refactor-build-request-body

Conversation

@StarsExpress

@StarsExpress StarsExpress commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Refs issue #461 1st point: deduplicate body construction for code_puppy/gemini_model.py::GeminiModel.request(), GeminiModel.request_stream().

Extraction

Inside GeminiModel a shared method joins:

    def _build_request_body(
        self,
        system_instruction,
        contents,
        model_settings,
        model_request_parameters,
        *,
        streaming: bool,
    ) -> dict[str, Any]:
        body: dict[str, Any] = {"contents": contents}

        gen_config = self._build_generation_config(model_settings)
        if gen_config:
            body["generationConfig"] = gen_config

        if system_instruction:
            body["systemInstruction"] = system_instruction

        # Add tools.
        if model_request_parameters.function_tools:
            body["tools"] = self._build_tools(model_request_parameters.function_tools)

            if streaming:
                body["toolConfig"] = {
                    "functionCallingConfig": {
                        "mode": "AUTO",
                        "streamFunctionCallArguments": True,
                    }
                }

        return body

How GeminiModel.request() calls GeminiModel._build_request_body():

body = self._build_request_body(
            system_instruction=system_instruction,
            contents=contents,
            model_settings=model_settings,
            model_request_parameters=model_request_parameters,
            streaming=False,
        )

How GeminiModel.request_stream() calls GeminiModel._build_request_body():

body = self._build_request_body(
            system_instruction=system_instruction,
            contents=contents,
            model_settings=model_settings,
            model_request_parameters=model_request_parameters,
            streaming=True,
        )

Next step

After PR #899 gets approval and joins main, I will post another PR for #461 2nd point.

@StarsExpress
StarsExpress marked this pull request as ready for review August 31, 2026 18:10
@StarsExpress StarsExpress changed the title refactor: extract request body for gemini_model.py. refactor: extract request body for gemini_model.py. Sep 5, 2026
@mpfaffenberger
mpfaffenberger merged commit c8b869b into mpfaffenberger:main Sep 10, 2026
3 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.

2 participants