MCPcopy
hub / github.com/langroid/langroid / to_string

Function to_string

langroid/utils/types.py:48–72  ·  view source on GitHub ↗

Best-effort conversion of arbitrary msg to str. Return empty string if conversion fails.

(msg: Any)

Source from the content-addressed store, hash-verified

46
47
48def to_string(msg: Any) -> str:
49 """
50 Best-effort conversion of arbitrary msg to str.
51 Return empty string if conversion fails.
52 """
53 if msg is None:
54 return ""
55 if isinstance(msg, str):
56 return msg
57 if isinstance(msg, BaseModel):
58 return msg.model_dump_json()
59 # last resort: use json.dumps() or str() to make it a str
60 try:
61 return json.dumps(msg)
62 except Exception:
63 try:
64 return str(msg)
65 except Exception as e:
66 logger.error(
67 f"""
68 Error converting msg to str: {e}",
69 """,
70 exc_info=True,
71 )
72 return ""
73
74
75def from_string(

Callers 6

_responseMethod · 0.90
_response_asyncMethod · 0.90
resultMethod · 0.90
to_ChatDocumentMethod · 0.90
to_LLMMessageMethod · 0.90
responseMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…