Functions

hunch.output

Appends the given values to the output file. This function has a similar signature to the built-in print.

Parameters:

  • *values object : The values to be written to the output.
  • sep Optional[str] : The separator between values. Defaults to a space.
  • end Optional[str] : The string appended after the last value. Defaults to a newline.

Example:

import hunch

# Appends "Hello World\n\n" to the output file
hunch.output("Hello", "World", sep=" ", end="\n\n")

hunch.input

Loads and returns the input JSON.

Returns:

  • Input : The validated input data

Example:

import hunch

# Fetch the input data
input_data = hunch.input()

hunch.compiled_prompt

Gets the entire compiled prompt.

Parameters:

  • instructions bool : Whether to include the instructions of the code runner block. Defaults to false.
  • titles bool : Whether to include the titles of the blocks. Defaults to true.

Returns:

  • str : The compiled prompt

Example:

import hunch

prompt = hunch.compiled_prompt(instructions=True, titles=False)

hunch.extract_url

Extracts the first URL from the upstream blocks.

Returns:

  • str : The first URL found.

Raises:

  • Error : If no URL is found

Example:

import hunch

url = hunch.extract_url()

hunch.extract_urls

Extracts all URLs from the upstream blocks.

Returns:

  • list[str] : A list of all URLs found

Example:

import hunch

urls = hunch.extract_urls()

hunch.block

Fetches an upstream block from the input.

Parameters:

  • title Optional[str] : The title of the block to fetch
  • ititle Optional[str] : The case-insensitive title of the block to fetch

Returns:

Raises:

  • Error : If the specified block is not found or if invalid arguments are provided.

Example:

import hunch

# Get by exact title
b = hunch.block(title="Introduction")

# Get by case-insensitive title
b = hunch.block(ititle="introduction")

Classes

class hunch.Input

Attributes

  • instructions Optional[str] : The instructions on the code runner block
  • files Optional[list[File]] : The files contained in the instructions
  • blocks list[Block] : The blocks upstream of the code runner

class hunch.Block

Attributes

  • title str : The title of the block.
  • output str : The output of the block or the content if the block is a content block
  • files Optional[list[File]] : The files contained in the block output

Methods

  • extract_url(self) -> str : Extracts the first URL from the block output
  • extract_urls(self) -> list[str] : Extracts all URLs from the block output

class hunch.File

Represents a file contained in a block.

Attributes

  • media_type Optional[str] : The media type of the file (e.g. “application/pdf”, “image/jpeg”)
  • url str : The URL to download the file
  • path Optional[Path]: The path to the file, if it has been downloaded

Methods

  • download(self, path: str | Path) -> None: Downloads the file to the given path and updates the path attribute

class hunch.Error

Raised when a hunch module specific error occurs.

Example:

import hunch

try:
    url = hunch.extract_url()
except hunch.Error:
    # no url found ...