MCPcopy Create free account
hub / github.com/VectifyAI/OpenKB / _coerce_language

Function _coerce_language

openkb/cli.py:817–841  ·  view source on GitHub ↗

Strip a language string; treat blanks as unset; reject unsafe values. The language string is interpolated into LLM system prompts (see ``_SYSTEM_TEMPLATE`` in ``openkb/agent/compiler.py`` and the query agent's instructions), so values with newlines or excessive length would let an e

(value: str | None)

Source from the content-addressed store, hash-verified

815
816
817def _coerce_language(value: str | None) -> str | None:
818 """Strip a language string; treat blanks as unset; reject unsafe values.
819
820 The language string is interpolated into LLM system prompts (see
821 ``_SYSTEM_TEMPLATE`` in ``openkb/agent/compiler.py`` and the query agent's
822 instructions), so values with newlines or excessive length would let an
823 external caller smuggle instructions into the prompt. Capping at
824 ``_LANGUAGE_MAX_LEN`` and rejecting control characters is enough to close
825 that vector while still allowing common forms ("en", "ko", "Korean",
826 "Simplified Chinese").
827
828 Returns the cleaned string, or ``None`` if the input was missing or blank
829 after stripping. Raises ``click.BadParameter`` on unsafe input.
830 """
831 if value is None:
832 return None
833 value = value.strip()
834 if not value:
835 return None
836 if len(value) > _LANGUAGE_MAX_LEN or any(c in value for c in "\n\r\t"):
837 raise click.BadParameter(
838 f"language must be {_LANGUAGE_MAX_LEN} characters or fewer with no control characters",
839 param_hint="'--language'",
840 )
841 return value
842
843
844def _language_option_callback(_ctx, _param, value):

Callers 2

initFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected