Home / Function/ format() — langchain Function Reference

format() — langchain Function Reference

Architecture documentation for the format() function in image.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  dedbfd05_46bc_9cd0_b34a_11d767ba397b["format()"]
  6a465cb4_c33a_e368_ad81_def98b4d2da2["ImagePromptTemplate"]
  dedbfd05_46bc_9cd0_b34a_11d767ba397b -->|defined in| 6a465cb4_c33a_e368_ad81_def98b4d2da2
  24bd1a9c_f02c_2911_efd9_da075dd6cda5["format_prompt()"]
  24bd1a9c_f02c_2911_efd9_da075dd6cda5 -->|calls| dedbfd05_46bc_9cd0_b34a_11d767ba397b
  style dedbfd05_46bc_9cd0_b34a_11d767ba397b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/prompts/image.py lines 84–132

    def format(
        self,
        **kwargs: Any,
    ) -> ImageURL:
        """Format the prompt with the inputs.

        Args:
            **kwargs: Any arguments to be passed to the prompt template.

        Returns:
            A formatted string.

        Raises:
            ValueError: If the url is not provided.
            ValueError: If the url is not a string.
            ValueError: If `'path'` is provided in the template or kwargs.

        Example:
            ```python
            prompt.format(variable1="foo")
            ```
        """
        formatted = {}
        for k, v in self.template.items():
            if isinstance(v, str):
                formatted[k] = DEFAULT_FORMATTER_MAPPING[self.template_format](
                    v, **kwargs
                )
            else:
                formatted[k] = v
        url = kwargs.get("url") or formatted.get("url")
        if kwargs.get("path") or formatted.get("path"):
            msg = (
                "Loading images from 'path' has been removed as of 0.3.15 for security "
                "reasons. Please specify images by 'url'."
            )
            raise ValueError(msg)
        detail = kwargs.get("detail") or formatted.get("detail")
        if not url:
            msg = "Must provide url."
            raise ValueError(msg)
        if not isinstance(url, str):
            msg = "url must be a string."
            raise ValueError(msg)  # noqa: TRY004
        output: ImageURL = {"url": url}
        if detail:
            # Don't check literal values here: let the API check them
            output["detail"] = cast("Literal['auto', 'low', 'high']", detail)
        return output

Subdomains

Called By

Frequently Asked Questions

What does format() do?
format() is a function in the langchain codebase, defined in libs/core/langchain_core/prompts/image.py.
Where is format() defined?
format() is defined in libs/core/langchain_core/prompts/image.py at line 84.
What calls format()?
format() is called by 1 function(s): format_prompt.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free